Php httpClient.execute上的程序块:NullPointerException

Php httpClient.execute上的程序块:NullPointerException,php,android,json,http,nullpointerexception,Php,Android,Json,Http,Nullpointerexception,我的android程序试图从localhost中的数据库获取一些数据,为此,我使用AsynkTask类和JSONParser类。如果调试程序,我可以到达httpResponse httpResponse=httpClient.executehttpPost;:我不能去下一个 public JSONObject makeHttpRequest(String url, String method, List<NameValuePair> params) { // Se

我的android程序试图从localhost中的数据库获取一些数据,为此,我使用AsynkTask类和JSONParser类。如果调试程序,我可以到达httpResponse httpResponse=httpClient.executehttpPost;:我不能去下一个

    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;

}
该NPE在FragmentControlAcorsa类的第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.getIntTAG_success;。这是NPE。 在my.php文件中有标记tag_SUCCESS,那么为什么会出现此异常呢?

如果在返回中定义了tag_SUCCESS,则需要引号,如果在Java中本地定义了tag_SUCCESS,则不需要引号

例如:

PHP

爪哇

您将得到一个空指针异常,因为Java正在寻找一个名为TAG_SUCCESS的本地定义变量

例如:

PHP

爪哇


如前所述,错误在于清单中没有internet权限


由于这两个答案,我发现了错误:和

当执行失败时,e.printStackTrace的结果是什么?您试图访问的url是什么?我想您的意思是success=json.getIntTAG\u success;1-如何在LogCat中找到printStackTrace?然而,我有这个:01-17 12:12:58.9051142-1159/?W/System.err﹕ 在com.example.app.JSONParser.makeHttpRequestJSONParser.java:145 01-17 12:12:59.075 1142-1159/?E/JSON解析器﹕ 分析数据org.json.JSONException时出错:输入结束,字符为0,共2个-此URL:http://10.0.2.2/PrimaAppAndroid/get/ultimaTratta.php 3-TAG_SUCCESS是一个字符串,其值为SUCCESS尝试在浏览器中点击此url,看看响应是什么事实上这就是你所说的,TAG_SUCCESS是一个变量。现在我编辑帖子并添加这个变量的声明。啊,好的。那么,需要明确的是,初始化了TAG_SUCCESS之后,您是否得到了错误?是的,如果您查看问题中的最后一个源文件,在开始时,我添加了初始化。
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();
        }
    }
}
echo json_encode(array('myInt' => 1));
int example;
example= json.getInt("myInt");
echo json_encode(array('myInt' => 1));
String myInt = "myInt";
int example;
example= json.getInt(myInt);