2011-12-15 194 views
8

我必須在以下URL數據發送到web服務如何使用JSON將數據發佈到Web服務?

要被髮送的數據的格式是:

new_member=[{"email":"[email protected]","username":"himanshu01","pwd":"himanshu01"}] 

其中emailusernamepwd是關鍵,並且具有相應的值的字符串通過的EditText取出串。我點擊按鈕發佈這些數據。

我沒有得到任何迴應bcoz我試圖反擊檢查與我的合作開發人員在他的iPhone相同的應用程序iPhone版的用戶名和密碼無效是由服務器說。

Signup.java類是:

Button b1=(Button)findViewById(R.id.button1); 
    b1.setOnClickListener(new OnClickListener() 
    { 
     EditText e=(EditText)findViewById(R.id.editText1); 
     String a=e.getText().toString(); 
     EditText e1=(EditText)findViewById(R.id.editText1); 
     String b=e1.getText().toString(); 
     EditText e2=(EditText)findViewById(R.id.editText1); 
     String c=e2.getText().toString(); 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      HttpClient client = new DefaultHttpClient(); 
       HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit 
       HttpResponse response; 
       JSONObject json = new JSONObject(); 
       try{ 
        HttpPost post = new HttpPost("URL"); 
        //System.out.println(post); 
        json.put("email", a); 
        json.put("username", b); 
        json.put("pwd",c); 
        StringEntity se = new StringEntity("JSON: " + json.toString()); 
        se.setContentEncoding(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 
//System.out.println(response); 
       } 
       } 
       catch(Exception e){ 
        e.printStackTrace(); 
        System.out.println(e.toString()); 
        //createDialog("Error", "Cannot Estabilish Connection"); 
       } 
     } 

    } 
      ); 

請幫助我,我在JSON一個新手和Android.Thanx解析。

回答

10

這裏是例子

HttpClient httpClient = new DefaultHttpClient(); 
HttpPost httpPost = new HttpPost(
    "https://api.dailymile.com/entries.json?oauth_token=" 
    + token); 

httpPost.setHeader("content-type", "application/json"); 
JSONObject data = new JSONObject(); 

data.put("message", dailyMilePost.getMessage()); 
JSONObject workoutData = new JSONObject(); 
data.put("workout", workoutData); 
workoutData.put("activity_type", dailyMilePost.getActivityType()); 
workoutData.put("completed_at", dailyMilePost.getCompletedAt()); 
JSONObject distanceData = new JSONObject(); 
workoutData.put("distance", distanceData); 
distanceData.put("value", dailyMilePost.getDistanceValue()); 
distanceData.put("units", dailyMilePost.getDistanceUnits()); 
workoutData.put("duration", dailyMilePost.getDurationInSeconds()); 
workoutData.put("title", dailyMilePost.getTitle()); 
workoutData.put("felt", dailyMilePost.getFelt()); 

StringEntity entity = new StringEntity(data.toString(), HTTP.UTF_8); 
httpPost.setEntity(entity); 

HttpResponse response = httpClient.execute(httpPost); 

請參閱此博客的完整信息http://simpleprogrammer.com/2011/06/04/oauth-and-rest-in-android-part-2/

+0

Thanx的答覆,請告訴我我是如何通過new_member =因爲我追加在網址,但實際提供的網址是http://www.fourerr.com/ws/signup我應該把新會員作爲JSON目的。??請幫助我.. – 2011-12-15 12:52:25

2

塊括號表示一個JSON數組,所以你只需要將你的json對象包裝在一個JSONArray中。

JSONArray array = new JSONArray(); 
JSONObject json = new JSONObject(); 
json.put("email", a); 
json.put("username", b); 
json.put("pwd",c); 
array.put(json); 

array放入實體中。

重要提示:您不應在主線程上執行長時間運行(例如網絡)任務。使用AsyncTask在後臺正確執行長時間運行的任務,然後更新UI。

+0

Thanx的回覆請告訴我我是如何通過new_member =因爲我在追加url但實際提供的網址是http://www.fourerr.com/ws/signup我應該把newmember當成json對象嗎?請幫助我.. – 2011-12-15 12:51:56

1

首先,您需要將codeonClick()方法內的StringsEditTexts中取出。

那麼它看起來像服務器需要一個JSONArray只有一個項目,所以你需要創建一個JSONArray,是這樣的:

JSONArray jsonArray=new JSONArray(); 
jsonArray.put(json); //your current json 
StringEntity entity=new StringEntity("new_member="+jsonArray); 
4
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3); 

nameValuePairs.add(new BasicNameValuePair("email", "[email protected]")); 
nameValuePairs.add(new BasicNameValuePair("username", "himanshu01")); 
nameValuePairs.add(new BasicNameValuePair("pwd", "himanshu01")); 

// You have to add your parameters as nameValuePairs 

String res = ""; 
       try 
       { 
        HttpClient httpclient = new DefaultHttpClient(); 
        HttpPost httppost = new HttpPost("URL"); 

          // Add your data 

          httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
          HttpResponse response = httpclient.execute(httppost); 
          res = EntityUtils.toString(response.getEntity()); 
          JSONTokener t = new JSONTokener(res); 
          JSONArray a = new JSONArray(t); 
          JSONObject o = a.getJSONObject(0); 
          String sc = o.getString("success"); 
          if(sc.equals("1")) 
          { 
           // posted successfully 
          } 
else 
{ 
// error occurred 
} 
       } 
       catch (Exception e) 
       { 

        e.printStackTrace(); 
       } 

不要忘記指定android.permission.INTERNET權限

相關問題