错误转换(JSON)结果

时间:2014-03-03 12:12:01

标签: php android mysql json

我想从mysql数据库中获取一些数据 PHP代码:

<?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();
?>

如果您执行该文件,它看起来像这样:

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

我的大问题是,我得到FATAL EXCEPTION: AsyncTask #1

这是我的代码:

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());
        }
        }
    }
}

我不知道我能做什么。如果你能帮助我,我会很高兴:)

这些是错误。

enter image description here

2 个答案:

答案 0 :(得分:2)

试试这个..

您已在全局变量中提供String URL2;,然后

再次在onCreate中,您已String URL2 = "http://domain.com/real.php";

check内部{{1>} AsyncTask被叫下载网页任务 x = task.execute(URL2).get(); URL2String为空,然后它会给出 NPE

只需移除onCreate内的URL2 = "http://domain.com/real.php";,如{{1}}

答案 1 :(得分:0)

检查你的http调用是否从调试器返回数据正确数据。如果是,那么请使用以下代码来读取字符串。

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

不要在异步任务上调用此get方法

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

替换为

  x = task.execute(URL2);

尝试从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());

            }