-1

Php Json の値を読み取ると、Eclipse にはエラーが表示されませんが、機能しません。走らなきゃいけないから頭おかしくなっちゃったんだけど、手伝ってくれる?実行しても何も起こりません。

これは Java アクティビティ コードです。

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

これが iatek.eu/sys/getsms.php への json コードです。

{"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"}]}

誰かが間違いを教えてもらえますか?ありがとう

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 に答える