2013-05-04 133 views
0

我是android新手,學習了一下。我很難在android和php之間傳遞json對象。如何解析android-php json webservice響應

我有這個代碼發送json對象發送json對象到服務器。它的工作很好。它也印在吐司上。但任何請幫助我如何閱讀並顯示我的服務器對我的應用程序的響應?

我的代碼看起來像

package com.test.webservice; 


import android.util.Log; 
import android.view.Menu; 
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 java.util.ArrayList; 

import android.widget.TextView; 
import android.widget.Toast; 

import org.apache.http.Header; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.ResponseHandler; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.entity.StringEntity; 
import org.apache.http.impl.client.BasicResponseHandler; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicHeader; 
import org.apache.http.params.HttpConnectionParams; 
import org.apache.http.protocol.HTTP; 
import org.apache.http.util.EntityUtils; 
import org.json.JSONObject; 

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 



     String path = "http://www.mysite.com/app.php"; 

     HttpClient client = new DefaultHttpClient(); 
     HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); // Timeout 
                       // Limit 
     HttpResponse response; 
     JSONObject json = new JSONObject(); 
     try { 
      HttpPost post = new HttpPost(path); 
      json.put("service", "GOOGLE"); 
      json.put("name", "My Name"); 
      Log.i("jason Object", json.toString()); 
      post.setHeader("json", json.toString()); 
      StringEntity se = new StringEntity(json.toString()); 
      se.setContentEncoding((Header) new BasicHeader(HTTP.CONTENT_TYPE, 
        "application/json")); 
      post.setEntity(se); 
      response = client.execute(post); 
      /* Checking response */ 
      if (response != null) { 
       InputStream in = response.getEntity().getContent(); // Get the 
                    // data in 
                     // the 
                     // entity 
       String a = convertStreamToString(in); 

       Toast.makeText(getApplicationContext(), 
         a, Toast.LENGTH_LONG).show(); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 






    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 

    private static String convertStreamToString(InputStream is) { 

     BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
     StringBuilder sb = new StringBuilder(); 

     String line = null; 
     try { 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       is.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     return sb.toString(); 
    } 

} 
+0

你是什麼意思顯示響應?你的意思是在PHP代碼,以便它應該返回json? – Kanth 2013-05-04 07:31:33

+0

感謝您的評論...沒有不是PHP代碼...我的意思是從PHP的反應,並解析它到數組,並顯示 – ramesh 2013-05-04 07:33:43

回答

1

假設,如果你有一個數組稱爲responsephp側像的下方,那麼我想你將數據填充到數組中這樣說:

$response = array(); 
$response["intvalue"] = 1234; 
$response["stringmessage"] = "Data from the server"; 

echo json_encode($response);// Here you are echoing json response. So in the inputstream you would get the json data. You need to extract it over there and can display according to your requirement. 

在android側提取:

在這裏您需要將S特林在下面的方式(你在祝酒顯示),以JSON對象:

JSONObject json = stringToJsonobj(a); // a is your string response 

int passedinteger = json.getInt("intvalue"); 
String passedStringValue = json.getString("stringmessage"); 

public JSONObject stringToJsonobj(String result) 
    { 
     JSONObject jObj = null; 
      try { 

      jObj = new JSONObject(result);   

     } catch (JSONException e) { 
      Log.e("JSON Parser", "Error parsing data " + e.toString()); 

     } 

     return jObj; 
} 

通過這種方式,你可以得到一個individual values(passedinteger, passedstringvalue)並顯示你想要的任何意見。希望這可以幫助

+0

工作...謝謝 – ramesh 2013-05-04 08:26:35