简体   繁体   中英

Error Converting (JSON) result

I want to get some data from a mysql database.
The PHP code:

<?php
$verbindung = mysql_connect ("",
"", "")
or die ("keine Verbindung möglich.
 Benutzername oder Passwort sind falsch");

mysql_select_db("DB1539594")
or die ("Die Datenbank existiert nicht.");

$q=mysql_query("SELECT * FROM status WHERE ID>'".$_REQUEST['ID']."'");
while($e=mysql_fetch_assoc($q))
        $output[]=$e;

print(json_encode($output));

mysql_close();
?>

If you execute the file it looks like this:

[{"ID":"1","name":"tester","status":"1","reports":"5","lastTime":"2014-02-27 17:48:25"}]

My big problem is, that i get a FATAL EXCEPTION: AsyncTask #1

Here's my code:

public class PublicCheck extends Activity {
    String URL2;
    String result;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_public_check);
        URL2 = "http://domain.com/real.php";
    }


    public void check() {
        DownloadWebPageTask task = new DownloadWebPageTask();
        String x;

             task.execute(URL2);


        //Log.i("Ergebnis", x+ URL2);

    }

    class DownloadWebPageTask extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... urls) {

                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(
                        urls[0]);
                try {

                     HttpResponse response = httpclient.execute(httppost);
                        result = inputStreamToString(
                                  response.getEntity().getContent()).toString();
                               }

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

            return null;
        }

                private StringBuilder inputStreamToString(InputStream is) {
                       String rLine = "";
                       StringBuilder answer = new StringBuilder();
                       BufferedReader rd = new BufferedReader(new InputStreamReader(is));

                       try {
                        while ((rLine = rd.readLine()) != null) {
                         answer.append(rLine);
                        }
                       }

                       catch (IOException e) {
                        // e.printStackTrace();
                        Toast.makeText(getApplicationContext(),
                          "Error..." + e.toString(), Toast.LENGTH_LONG).show();
                       }
                       return answer;
                      }




        @Override
        protected void onPostExecute(String result) {
            try{
                JSONArray jArray = new JSONArray(result);
                for(int i=0;i<jArray.length();i++){
                        JSONObject json_data = jArray.getJSONObject(i);
                        Log.i("log_tag","id: "+json_data.getInt("ID")+
                                ", name: "+json_data.getString("name")+
                                ", status: "+json_data.getInt("status")+
                                ", reports: "+json_data.getInt("reports")
                        );
                }
        }
        catch(JSONException e){
                Log.e("log_tag", "Error parsing data "+e.toString());
        }
        }
    }
}

I have no idea what i could do. I would be very pleased if you could help me :)

These are the errors.

在此输入图像描述

Try this..

You have given String URL2; in Global variable and then

In your onCreate again you have given String URL2 = "http://domain.com/real.php";

Inside check your called DownloadWebPageTask AsyncTask x = task.execute(URL2).get(); that URL2 is null then it'll give NPE

Just Remove String inside onCreate like URL2 = "http://domain.com/real.php";

check is your http call is returning data proper data or not from debugger. If yes then please use following code for reading string.

   HttpResponse response = httpclient.execute(httpget);
   EntityUtils.toString(response.getEntity());

dont call this get method on async task

 x = task.execute(URL2).get();

replace with

  x = task.execute(URL2);

try this for reading data from url

try {
                HttpPost httppost = new HttpPost(
                        urls[0]);
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response1 = httpclient.execute(httppost);
                if( response1 != null)
        {
                        HttpEntity httpEntity = response1.getEntity();
                        result =  EntityUtils.toString(httpEntity);
                } 
    }
    catch (Exception e) 
    {
                Log.e("log_tag", "Error converting result " + e.toString());

            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM