-1

Reading Php Json values, Eclipse dont see the error but it doesnt work. Im becoming crazy because it must run, can you help me? When i execute it nothing happens.

This is the java activity code:

  package com.json.php;

import android.app.Activity;
import android.os.Bundle;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import android.widget.TextView;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


public class JSONExampleActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        HttpClient httpclient = new DefaultHttpClient();
       HttpPost httppost = new HttpPost("http://iatek.eu/sys/getsms.php");

        TextView textView = (TextView)findViewById(R.id.textView1);
        try {

            HttpResponse response = httpclient.execute(httppost);           
            String jsonResult = inputStreamToString(response.getEntity().getContent()).toString();                      
            JSONObject obj = new JSONObject(jsonResult);
            JSONArray jsonArray = obj.getJSONArray("posts");

            /*Para hacer prueba accedo a un registro concreto en este caso el 3*/
               JSONObject childJSONObject = jsonArray.getJSONObject(3);
                 String username = childJSONObject.getString("username");
                 String sms = childJSONObject.getString("sms");
                 String fcat = childJSONObject.getString("fcat");
                 textView.setText(""+sms+"--" + username);

            /* para hacer pruebas lo he comentado
            for (int i = 0; i < jsonArray.length(); i++) 
            {
                 JSONObject childJSONObject = jsonArray.getJSONObject(i);
                 String username = childJSONObject.getString("username");
                 String sms = childJSONObject.getString("sms");
                 String fcat = childJSONObject.getString("fcat");
                 textView.setText(""+sms+"--" + username);
            }*/



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


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

and here is the json code into iatek.eu/sys/getsms.php

{"posts":[{"cid":"11","username":"Livi","sms":"ag","fto":"","fcat":"cat"},{"cid":"10","username":"Sumone","sms":"","fto":"","fcat":""},{"cid":"9","username":"R2D2","sms":"dw","fto":"wd","fcat":"wd"},{"cid":"5","username":"Roy","sms":"sa","fto":"sa","fcat":"sa"},{"cid":"12","username":"Charles","sms":"ag","fto":"","fcat":"cat"},{"cid":"13","username":"Clarck","sms":"age","fto":"","fcat":"cat"}]}

can someone tell me where is the mistake? thanks

4

2 回答 2

0

解决!!!!尽善尽美

这是代码

非常感谢你

package com.json.php;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.widget.EditText;
import android.widget.TextView;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.json.php.R;

public class JSONExampleActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        new GetSmsTask().execute("http://YOURWEBSITE.eu/sys/getsms.php");
       }   

    private class GetSmsTask extends AsyncTask<String, Void, JSONObject> {
     protected JSONObject doInBackground(String... urls) {
     JSONObject obj = null;
     try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(urls[0]);
            HttpResponse response = httpclient.execute(httppost);           
         /*   String jsonResult = "{\"posts\":" + //FOR TESTS
                    "[{\"cid\":\"11\",\"username\":\"Livi\",\"sms\":\"ag\",\"fto\":\"\",\"fcat\":\"cat\"}," +
                    " {\"cid\":\"11\",\"username\":\"Livi\",\"sms\":\"ag\",\"fto\":\"\",\"fcat\":\"cat\"}, " +
                    "{\"cid\":\"11\",\"username\":\"Livi\",\"sms\":\"ag\",\"fto\":\"\",\"fcat\":\"cat\"}," +
                    "{\"cid\":\"11\",\"username\":\"Livi\",\"sms\":\"ag\",\"fto\":\"\",\"fcat\":\"cat\"}]}";                     

            */
            String jsonResult = inputStreamToString(response.getEntity().getContent()).toString(); 
            obj = new JSONObject(jsonResult);
        } 
        catch (JSONException e) {
            e.printStackTrace();
        } 
        catch (ClientProtocolException e) {
            e.printStackTrace();
        } 
        catch (IOException e) {
            e.printStackTrace();
        }

        return obj;

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

     protected void onPostExecute(JSONObject obj) {
      JSONArray jsonArray;
    try {
        jsonArray = obj.getJSONArray("posts");
          JSONObject childJSONObject = jsonArray.getJSONObject(3);
             String username = childJSONObject.getString("username");
             String sms = childJSONObject.getString("sms");
             String fcat = childJSONObject.getString("fcat");
             TextView textView = (TextView) findViewById(R.id.textView1);
            textView.setText(""+sms+"--" + username);
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


     }
 }

}
于 2013-09-14T01:18:41.527 回答
0

您不能在主线程上运行网络操作。

请改用asyncTask

public class JSONExampleActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        new GetSmsTask().execute("http://iatek.eu/sys/getsms.php");
       }   

    private class GetSmsTask extends AsyncTask<String, Void, JSONObject> {
     protected JSONObject doInBackground(String... urls) {
     JSONObject obj = null;
     try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url[0]);
            HttpResponse response = httpclient.execute(httppost);           
            String jsonResult = inputStreamToString(response.getEntity()
                                                   .getContent()).toString();                      
            obj = new JSONObject(jsonResult);
        } 
        catch (JSONException e) {
            e.printStackTrace();
        } 
        catch (ClientProtocolException e) {
            e.printStackTrace();
        } 
        catch (IOException e) {
            e.printStackTrace();
        }

        return obj;
     }

     protected void onPostExecute(JSONObject obj) {
      JSONArray jsonArray = obj.getJSONArray("posts");

       JSONObject childJSONObject = jsonArray.getJSONObject(3);
         String username = childJSONObject.getString("username");
         String sms = childJSONObject.getString("sms");
         String fcat = childJSONObject.getString("fcat");
         textView.setText(""+sms+"--" + username);
     }
 }

}

编辑:

我刚刚自己运行了代码,我得到了这个

09-13 13:33:59.315: W/System.err(14200): org.json.JSONException: {"posts":[{......

这意味着您的 JSON 无效

http://pro.jsonlint.com/将您的链接粘贴到那里,您将看到错误。

要测试代码是否正常工作,请替换此

String jsonResult = inputStreamToString(response.getEntity()
                     .getContent()).toString(); 

有了这个:

String jsonResult = "{\"posts\":" +
    "[{\"cid\":\"11\",\"username\":\"Livi\",\"sms\":\"ag\",\"fto\":\"\",\"fcat\":\"cat\"}," +
    " {\"cid\":\"11\",\"username\":\"Livi\",\"sms\":\"ag\",\"fto\":\"\",\"fcat\":\"cat\"}, " +
    "{\"cid\":\"11\",\"username\":\"Livi\",\"sms\":\"ag\",\"fto\":\"\",\"fcat\":\"cat\"}," +
    "{\"cid\":\"11\",\"username\":\"Livi\",\"sms\":\"ag\",\"fto\":\"\",\"fcat\":\"cat\"}]}";
于 2013-09-13T14:57:26.090 回答