我的android程序试图从localhost中的数据库中获取一些数据,为此我使用AsynkTask
类和JSONParser
类。如果我调试程序,我可以到达HttpResponse httpResponse = httpClient.execute(httpPost);
行:我不能去下一行。
public JSONObject makeHttpRequest(String url, String method, List<NameValuePair> params) {
// Se mancano dei commenti, andare a quelli del metodo precedente.
// If there aren't some comments, go to the ones of the previous method.
// Making HTTP request
try {
// Controlla il metodo.
// Check for request method.
if(method == "POST"){
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
// Invio dei parametri.
// Passing the params.
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost); // LAST LINE WHERE I COME
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method == "GET"){
[...]
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
[...]
// return JSON String
return jObj;
}
如果我在LogCat
中过滤异常,我会得到一些NullPointerException:
01-17 12:09:06.800 382-434/system_process E/ConnectivityService﹕ Exception trying to add a route: java.lang.IllegalStateException: command '18 interface fwmark exempt add 10.0.2.2/32' failed with '400 18 Failed to add exemption rule (File exists)'
01-17 12:09:07.050 382-434/system_process E/ConnectivityService﹕ Exception trying to add a route: java.lang.IllegalStateException: command '20 interface fwmark exempt add 10.0.2.3/32' failed with '400 20 Failed to add exemption rule (File exists)'
01-17 12:09:07.070 382-434/system_process E/ConnectivityService﹕ Exception trying to add a route: java.lang.IllegalStateException: command '21 interface route add eth0 secondary 10.0.2.2 32 0.0.0.0' failed with '400 21 ip route modification failed (No such device)'
01-17 12:09:07.110 382-434/system_process E/ConnectivityService﹕ Exception trying to add a route: java.lang.IllegalStateException: command '22 interface route add eth0 secondary 0.0.0.0 0 10.0.2.2' failed with '400 22 ip route modification failed (No such device)'
01-17 12:09:11.820 382-681/system_process E/ConnectivityService﹕ Exception trying to add a route: java.lang.IllegalStateException: command '30 interface fwmark exempt add 10.0.2.2/32' failed with '400 30 Failed to add exemption rule (File exists)'
01-17 12:09:13.570 382-681/system_process E/ConnectivityService﹕ Exception trying to add a route: java.lang.IllegalStateException: command '32 interface fwmark exempt add 173.194.40.6/32' failed with '400 32 Failed to add exemption rule (File exists)'
01-17 12:09:16.740 382-681/system_process E/ConnectivityService﹕ Exception trying to add a route: java.lang.IllegalStateException: command '34 interface fwmark exempt add 10.0.2.2/32' failed with '400 34 Failed to add exemption rule (File exists)'
01-17 12:09:16.920 382-681/system_process E/ConnectivityService﹕ Exception trying to add a route: java.lang.IllegalStateException: command '36 interface fwmark exempt add 173.194.40.4/32' failed with '400 36 Failed to add exemption rule (File exists)'
01-17 12:09:20.320 382-681/system_process E/ConnectivityService﹕ Exception trying to add a route: java.lang.IllegalStateException: command '38 interface fwmark exempt add 10.0.2.2/32' failed with '400 38 Failed to add exemption rule (File exists)'
01-17 12:09:20.530 382-681/system_process E/ConnectivityService﹕ Exception trying to add a route: java.lang.IllegalStateException: command '40 interface fwmark exempt add 173.194.40.6/32' failed with '400 40 Failed to add exemption rule (File exists)'
01-17 12:09:23.850 382-681/system_process E/ConnectivityService﹕ Exception trying to add a route: java.lang.IllegalStateException: command '42 interface fwmark exempt add 10.0.2.2/32' failed with '400 42 Failed to add exemption rule (File exists)'
01-17 12:09:24.080 382-681/system_process E/ConnectivityService﹕ Exception trying to add a route: java.lang.IllegalStateException: command '44 interface fwmark exempt add 173.194.40.0/32' failed with '400 44 Failed to add exemption rule (File exists)'
01-17 12:12:59.065 1142-1159/? E/Buffer Error﹕ Error converting result java.lang.NullPointerException: lock == null
01-17 12:12:59.075 1142-1159/? E/JSON Parser﹕ Error parsing data org.json.JSONException: End of input at character 0 of
01-17 12:12:59.165 1142-1159/? E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #2
Process: com.example.app, PID: 1142
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.NullPointerException
at com.example.app.FragmentControllaCorsa$DownloadUltimaCorsa.doInBackground(FragmentControllaCorsa.java:179)
at com.example.app.FragmentControllaCorsa$DownloadUltimaCorsa.doInBackground(FragmentControllaCorsa.java:132)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
01-17 12:14:57.185 1197-1213/? E/Buffer Error﹕ Error converting result java.lang.NullPointerException: lock == null
01-17 12:14:57.185 1197-1213/? E/JSON Parser﹕ Error parsing data org.json.JSONException: End of input at character 0 of
01-17 12:14:57.205 1197-1213/? E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #2
Process: com.example.app, PID: 1197
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.NullPointerException
at com.example.app.FragmentControllaCorsa$DownloadUltimaCorsa.doInBackground(FragmentControllaCorsa.java:179)
at com.example.app.FragmentControllaCorsa$DownloadUltimaCorsa.doInBackground(FragmentControllaCorsa.java:132)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
01-17 12:15:27.805 1232-1246/? E/Buffer Error﹕ Error converting result java.lang.NullPointerException: lock == null
01-17 12:15:27.805 1232-1246/? E/JSON Parser﹕ Error parsing data org.json.JSONException: End of input at character 0 of
01-17 12:15:27.875 1232-1246/? E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #2
Process: com.example.app, PID: 1232
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.NullPointerException
at com.example.app.FragmentControllaCorsa$DownloadUltimaCorsa.doInBackground(FragmentControllaCorsa.java:179)
at com.example.app.FragmentControllaCorsa$DownloadUltimaCorsa.doInBackground(FragmentControllaCorsa.java:132)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
01-17 12:18:37.525 1285-1301/? E/Buffer Error﹕ Error converting result java.lang.NullPointerException: lock == null
01-17 12:18:37.525 1285-1301/? E/JSON Parser﹕ Error parsing data org.json.JSONException: End of input at character 0 of
01-17 12:18:37.675 1285-1301/? E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #2
Process: com.example.app, PID: 1285
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.NullPointerException
at com.example.app.FragmentControllaCorsa$DownloadUltimaCorsa.doInBackground(FragmentControllaCorsa.java:179)
at com.example.app.FragmentControllaCorsa$DownloadUltimaCorsa.doInBackground(FragmentControllaCorsa.java:132)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
01-17 12:24:49.535 1336-1352/? E/Buffer Error﹕ Error converting result java.lang.NullPointerException: lock == null
01-17 12:24:49.535 1336-1352/? E/JSON Parser﹕ Error parsing data org.json.JSONException: End of input at character 0 of
01-17 12:24:49.555 1336-1352/? E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #2
Process: com.example.app, PID: 1336
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.NullPointerException
at com.example.app.FragmentControllaCorsa$DownloadUltimaCorsa.doInBackground(FragmentControllaCorsa.java:179)
at com.example.app.FragmentControllaCorsa$DownloadUltimaCorsa.doInBackground(FragmentControllaCorsa.java:132)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
这个NPE在FragmentControllaCorsa类的第179和132行上升。在第132行,我在这里发布了内部类的声明:
class DownloadUltimaCorsa extends AsyncTask<Void, Void, JSONObject>{ // LINE 132
private ProgressDialog pDialog;
// Test dall'Emulatore:
// Testing from Emulator:
private static final String LOGIN_URL = "http://10.0.2.2/PrimaAppAndroid/get/ultimaTratta.php";
// Tag(ID) del responso Json restituito dallo script php:
// JSON element ids from repsonse of php script:
private static final String TAG_SUCCESS = "success";
private static final String TAG_DATA = "Data";
private static final String TAG_DURATA = "DurataConvalida";
/**
* Metodo che viene eseguito prima di doInBackground.
* Avvio il ProgressDialog.
* --
* Method executed before of doInBackgroung.
* I start the ProgressDialog.
*/
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(context);
pDialog.setMessage("Download ultima tratta...");
// Dura un tempo indeterminato.
// It lasts a undefined time.
pDialog.setIndeterminate(false);
// E' cancellabile dal tasto Back.
// It's cancelable by the Back key.
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONObject doInBackground(Void... voids) {
// Per il controllo se è andato a buon fine.
// Checking if all goes well.
int success;
// Il parametro da passare.
// The param to pass.
String codice = codiceDocumento.getText().toString();
JSONParser jsonParser = new JSONParser();
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>(); //http://stackoverflow.com/a/17609232/2337094 Namevaluepair
params.add(new BasicNameValuePair("codice", codice));
// Avvio della richiesta.
// Start the request.
JSONObject json = jsonParser.makeHttpRequest(
LOGIN_URL, "POST", params);
// json success tag
success = json.getInt(TAG_SUCCESS); // LINE 179
if (success == 1) {//SE VORRÒ GESTIRE L'ERRORE.
return json;
} else {
return json;
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* Metodo eseguito dopo doInBackground. Chiudo il ProgressDIalog.
* --
* Method executed after doInBackgroun. I Close the ProgressDialog.
* @param json
*/
@Override
protected void onPostExecute(JSONObject json) {
super.onPostExecute(json);
// Chiudo il ProgressDialog.
// Dismiss the ProgressDialog.
pDialog.dismiss();
try {
ultimaCorsa.setText(json.getString(TAG_DATA));
validitaCorsa.setText(json.getString(TAG_DURATA));
} catch (JSONException e) {
e.printStackTrace();
}
}
}
在第179行有success = json.getInt(TAG_SUCCESS);
。这是NPE的复兴。
在我的.php文件中有标记TAG_SUCCESS
,那么为什么会出现这个异常?
答案 0 :(得分:0)
如果在返回中定义TAG_SUCCESS
而不是您需要引号,如果它在Java中本地定义,则不需要引号。
示例:
PHP
echo json_encode(array('myInt' => 1));
爪哇
int example;
example= json.getInt("myInt");
您正在获取空指针异常,因为Java正在查找名为TAG_SUCCESS
示例:
PHP
echo json_encode(array('myInt' => 1));
爪哇
String myInt = "myInt";
int example;
example= json.getInt(myInt);
答案 1 :(得分:0)
错误是Manifest中没有互联网许可,正如here所述。
我发现了错误,感谢这两个答案:first one和second one