node.js에 post보내고 응답값을 json으로 받아오기
조회수 670회
제목 그대로 nodejs에 json형식으로 post보내고 그에대한 응답값을 받아오는걸 구현중입니다.!!!
//2020-08-29 04:19:53.003 20737-20737/com.example.application E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.application, PID: 20737
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference
at com.example.application.signupActivity$JSONTask.onPostExecute(signupActivity.java:139)
at com.example.application.signupActivity$JSONTask.onPostExecute(signupActivity.java:61)
at android.os.AsyncTask.finish(AsyncTask.java:755)
at android.os.AsyncTask.access$900(AsyncTask.java:192)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:772)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:8034)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1076)
이러한 오류가 계속 뜹니다ㅠㅠㅠ
public class JSONTask extends AsyncTask<String, String, String>{ //61
@Override
protected String doInBackground(String... urls){
String txtemail = email.getText().toString();
String txtusername = username.getText().toString();
String txtnickname = nickname.getText().toString();
String txtpassword = password.getText().toString();
String txtpassword2 = password2.getText().toString();
String txtmemnum = memnum.getText().toString();
try {
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("id",txtemail);
jsonObject.accumulate("pw",txtpassword);
jsonObject.accumulate("pw2",txtpassword2);
jsonObject.accumulate("nickName",txtnickname);
jsonObject.accumulate("userName",txtusername);
jsonObject.accumulate("memNum",txtmemnum);
HttpURLConnection connection =null;
BufferedReader reader = null;
try {
URL url = new URL(urls[0]);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Cache-Control","no-cache");
connection.setRequestProperty("Content-Type","application/json");
connection.setRequestProperty("Accept","application/json");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.connect();
OutputStream outputStream = connection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream));
writer.write(jsonObject.toString());
writer.flush();
writer.close();
InputStream stream = connection.getInputStream();
JSONObject jsonObject1=new JSONObject(stream.toString());
Object res_State = jsonObject1.get("res_State");
return res_State.toString();
} catch (IOException e){
e.printStackTrace();
} finally {
if (connection!=null){
connection.disconnect();
}
}
try {
if (reader!=null){
reader.close();
}
} catch (IOException e){
e.printStackTrace();
}
} catch (Exception e){
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result){
super.onPostExecute(result);
switch (result) { //139
case "id_is_invalid_form":
Toast.makeText(signupActivity.this, "올바른 이메일을 입력하세요", Toast.LENGTH_SHORT).show();
break;
case "pw_if_invalid_form":
Toast.makeText(signupActivity.this, "올바른 비밀번호를 입력하세요", Toast.LENGTH_SHORT).show();
break;
case "nickName_is_invalid_form":
Toast.makeText(signupActivity.this, "올바른 닉네임을 입력하세요", Toast.LENGTH_SHORT).show();
break;
case "userName_is_invalid_form":
Toast.makeText(signupActivity.this, "올바른 이름을 입력하세요", Toast.LENGTH_SHORT).show();
break;
case "pw_do_not_match":
Toast.makeText(signupActivity.this, "두 비밀번호가 일치하지 않습니다.", Toast.LENGTH_SHORT).show();
break;
case "email_is_already_used":
Toast.makeText(signupActivity.this, "사용중인 이메일 입니다.", Toast.LENGTH_SHORT).show();
break;
case "nickname_is_already_used":
Toast.makeText(signupActivity.this, "사용중인 닉네임 입니다.", Toast.LENGTH_SHORT).show();
break;
case "success":
Toast.makeText(signupActivity.this, "회원가입 되었습니다.", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(signupActivity.this, MainActivity.class);
startActivity(intent);
finish();
break;
default:
Toast.makeText(signupActivity.this, "회원가입에 실패했습니다..", Toast.LENGTH_SHORT).show();
break;
}
}
}
로그캣에 뜨는 61번과 139번은 코드에 함께 적어두었습니다! 조금만 도와주세요ㅠㅠㅠ
댓글 입력