簡體   English   中英

錯誤轉換(JSON)結果

[英]Error Converting (JSON) result

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

我不知道我能做什么。 如果你能幫助我,我會很高興:)

這些是錯誤。

在此輸入圖像描述

嘗試這個..

你給了String URL2; 在全局變量然后

再次在你的onCreate你給了String URL2 = "http://domain.com/real.php";

在里面check你的名為DownloadWebPageTask AsyncTask x = task.execute(URL2).get(); 如果URL2為null,那么它將給出NPE

只需刪除onCreate String ,如URL2 = "http://domain.com/real.php";

檢查是你的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());

            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM