gpt4 book ai didi

java - AsyncTask doInBackground() 方法中的空指针异常

转载 作者:行者123 更新时间:2023-12-01 11:57:09 24 4
gpt4 key购买 nike

我正在尝试使用 php 将从 Facebook api 获取的用户详细信息存储到数据库中。我使用 JSONParser 类来执行返回 JSON 对象的 POST。当我从 AsyncTask 中调用 JSONParser 类中的 makeHttpRequest 方法时另一个类,我得到一个空指针异常。我检查了我传递的任何参数是否为空,但它们不是并且无法理解为什么我无法获得响应。我试图从类似的答案中找到答案SO中提出的问题,但徒劳无功。请帮助我。谢谢

这是类中的 AsyncTask,它尝试调用 JSONparser 类中的 makeHTTPRequest 方法:

private class CreateUser extends AsyncTask<String, String, String> {

@Override
protected void onPreExecute() {
super.onPreExecute();

// Log.d("email", email);
pDialog = new ProgressDialog(FbProfileActivity.this);
pDialog.setMessage("Creating User...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}

@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
// Check for success tag
int success;

try {
// Building Parameters
// Log.d("param_username", username);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
// params.add(new BasicNameValuePair("age", age));
params.add(new BasicNameValuePair("gender",gender));
params.add(new BasicNameValuePair("email", email));
// params.add(new BasicNameValuePair("location", location));
Log.d("params","params" + params);


Log.d("JsonParser's URL", "params" +REGISTER_URL );
Log.d("request!", "starting",new Exception()); //Posting user data to script
JSONObject json = jsonParser.makeHttpRequest(REGISTER_URL, "POST", params);

// full json response
// Log.d("Registering attempt", json.toString());

// json success element
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("User Created", json.toString());
finish();
return json.getString(TAG_MESSAGE);
}else{
Log.d("Registering Failure!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);

}
} catch (JSONException e) {
e.printStackTrace();
}

return null;

}

protected void onPostExecute(String file_url) {
// dismiss the dialog once product deleted
pDialog.dismiss();
if (file_url != null){
Toast.makeText(FbProfileActivity.this, file_url, Toast.LENGTH_LONG).show();
}

}
}

这是我得到空指针异常的行:

JSONObject json = jsonParser.makeHttpRequest(REGISTER_URL, "POST", params);

这是 JSONParser 类中的 makeHttpRequest 方法:

public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
Log.d(TAG,"makeHttpRequest() is called");
// Making HTTP request
try {

// check for request method
if(method == "POST"){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));

HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();

}else if(method == "GET"){
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);

HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}

} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}

// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}

// return JSON String
return jObj;

}

堆栈跟踪:

02-06 19:28:13.720: D/request!(2247): starting
02-06 19:28:13.720: D/request!(2247): java.lang.Exception
02-06 19:28:13.720: D/request!(2247): at com.example.letschatindia.FbProfileActivity$CreateUser.doInBackground(FbProfileActivity.java:448)
02-06 19:28:13.720: D/request!(2247): at com.example.letschatindia.FbProfileActivity$CreateUser.doInBackground(FbProfileActivity.java:1)
02-06 19:28:13.720: D/request!(2247): at android.os.AsyncTask$2.call(AsyncTask.java:288)
02-06 19:28:13.720: D/request!(2247): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
02-06 19:28:13.720: D/request!(2247): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
02-06 19:28:13.720: D/request!(2247): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
02-06 19:28:13.720: D/request!(2247): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
02-06 19:28:13.720: D/request!(2247): at java.lang.Thread.run(Thread.java:841)
02-06 19:28:13.721: W/dalvikvm(2247): threadid=19: thread exiting with uncaught exception (group=0x4165fd40)
02-06 19:28:13.723: E/AndroidRuntime(2247): FATAL EXCEPTION: AsyncTask #5
02-06 19:28:13.723: E/AndroidRuntime(2247): Process: com.example.letschatindia, PID: 2247
02-06 19:28:13.723: E/AndroidRuntime(2247): java.lang.RuntimeException: An error occured while executing doInBackground()
02-06 19:28:13.723: E/AndroidRuntime(2247): at android.os.AsyncTask$3.done(AsyncTask.java:300)
02-06 19:28:13.723: E/AndroidRuntime(2247): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
02-06 19:28:13.723: E/AndroidRuntime(2247): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
02-06 19:28:13.723: E/AndroidRuntime(2247): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
02-06 19:28:13.723: E/AndroidRuntime(2247): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
02-06 19:28:13.723: E/AndroidRuntime(2247): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
02-06 19:28:13.723: E/AndroidRuntime(2247): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
02-06 19:28:13.723: E/AndroidRuntime(2247): at java.lang.Thread.run(Thread.java:841)
02-06 19:28:13.723: E/AndroidRuntime(2247): Caused by: java.lang.NullPointerException
02-06 19:28:13.723: E/AndroidRuntime(2247): at com.example.letschatindia.FbProfileActivity$CreateUser.doInBackground(FbProfileActivity.java:449)
02-06 19:28:13.723: E/AndroidRuntime(2247): at com.example.letschatindia.FbProfileActivity$CreateUser.doInBackground(FbProfileActivity.java:1)
02-06 19:28:13.723: E/AndroidRuntime(2247): at android.os.AsyncTask$2.call(AsyncTask.java:288)
02-06 19:28:13.723: E/AndroidRuntime(2247): at java.util.concurrent.FutureTask.run(FutureTask.java:237)

最佳答案

我认为 jsonParser 是 null 它必须在使用它之前实例化。

关于java - AsyncTask doInBackground() 方法中的空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28368518/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com