2014-03-03 154 views
0
public void send_Json(String url, JSONObject json,String userTken) { 

    try { 

    HttpParams httpParams = new BasicHttpParams(); 


    HttpConnectionParams.setConnectionTimeout(httpParams,TIMEOUT_MILLISEC); 
    HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC); 
    HttpClient client = new DefaultHttpClient(httpParams); 

    HttpPost request = new HttpPost(url); 

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); 
    nameValuePairs.add(new BasicNameValuePair("userToken", userTken)); 

    request.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 


    request.setEntity(new ByteArrayEntity(json.toString().getBytes(
      "UTF8"))); 



    request.setHeader("json", json.toString()); 



    HttpResponse response = client.execute(request); 
    HttpEntity entity = response.getEntity(); 
    // If the response does not enclose an entity, there is no need 
    if (entity != null) { 
     InputStream instream = entity.getContent(); 

     // String result = .convertStreamToString(instream); 
     Log.i("Read from server", "xcxcxcx"); 
     // Toast.makeText(context, "yeah", Toast.LENGTH_LONG).show(); 
    } 
} catch (Throwable t) { 
    Log.i("Read from server", "xcxcxcxcc cccccccccccc"); 
    // Toast.makeText(context, "Request failed: " + t.toString(), 
    // Toast.LENGTH_LONG).show(); 
} 
} 

我寫了這個代碼發送JSON對象與我的參數。但我需要知道如何發送它。我已經知道如何發送JSON對象。發送JSON對象

回答

1
nameValuePairs.add(new BasicNameValuePair("jsonKey", json.toString())); 

代碼

public void send_Json(String url, JSONObject json,String userTken) { 

    try { 

    HttpParams httpParams = new BasicHttpParams(); 


    HttpConnectionParams.setConnectionTimeout(httpParams,TIMEOUT_MILLISEC); 
    HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC); 
    HttpClient client = new DefaultHttpClient(httpParams); 

    HttpPost request = new HttpPost(url); 

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); 
    nameValuePairs.add(new BasicNameValuePair("userToken", userTken)); 
nameValuePairs.add(new BasicNameValuePair("jsonKey", json.toString())); 
    request.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 



    HttpResponse response = client.execute(request); 
    HttpEntity entity = response.getEntity(); 
    // If the response does not enclose an entity, there is no need 
    if (entity != null) { 
     InputStream instream = entity.getContent(); 

     // String result = .convertStreamToString(instream); 
     Log.i("Read from server", "xcxcxcx"); 
     // Toast.makeText(context, "yeah", Toast.LENGTH_LONG).show(); 
    } 
} catch (Throwable t) { 
    Log.i("Read from server", "xcxcxcxcc cccccccccccc"); 
    // Toast.makeText(context, "Request failed: " + t.toString(), 
    // Toast.LENGTH_LONG).show(); 
} 
}