okhttp post 질문입니다.
조회수 3729회
java.lang.NullPointerException: Attempt to invoke virtual method 'okhttp3.Call okhttp3.OkHttpClient.newCall(okhttp3.Request)' on a null object reference
지금 이런 에러가 뜨는데 get은 okhttp 를 통해 되는데 json 데이터를 post하는게 안되네요 ㅜㅜ 구글링해가면서 하는데 잘 안됩니다.
public class PostComments {
String doPostRequest(String url, String json) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
Log.e("tag", " asfasdfadfasdf " + json);
Request request = new Request.Builder()
.header("Content-Type", "application/json")
.url(url)
.post(body)
.build();
//Response response;
client2.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String res = response.body().string();
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
JSONObject jsonObject = new JSONObject(res);
String parentJArray = jsonObject.getString("responseCode");
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
});
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.writeCommentsBtn:
JSONObject jsonObject = new JSONObject();
if(content == null) {
Toast.makeText(getApplicationContext(), "내용을 입력하세요", Toast.LENGTH_SHORT).show();
} else {
try {
jsonObject.put("board_id", boardId);
jsonObject.put("content", content);
pjson = jsonObject.toString();
Log.e("tag", boardId + " " + content + " " +pjson);
} catch (JSONException e) {
e.printStackTrace();
}
PostComments postComments = new PostComments();
try {
postComments.doPostRequest("https://api.3rd.supply/comments", pjson);
} catch (IOException e) {
e.printStackTrace();
}
클릭 했을 때 통신을 하는데 저렇게 코딩을 했는데 왜 에러가 뜰까요? ㅜ null object 에러가 뜨는데 제가 데이터를 잘못보내고 있는건가요?
댓글 입력