Android 发送带有参数的json对象 public void send_Json(字符串url、JSONObject Json、字符串userTken){ 试一试{ HttpParams HttpParams=新的BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams,超时0毫秒); HttpConnectionParams.setSoTimeout(httpParams,超时0毫秒); HttpClient=新的默认HttpClient(httpParams); HttpPost请求=新的HttpPost(url); List nameValuePairs=新的ArrayList(1); 添加(新的BasicNameValuePair(“userToken”,userTken)); setEntity(新的UrlEncodedFormEntity(nameValuePairs)); setEntity(新的ByteArrayEntity(json.toString().getBytes( "UTF8);; setHeader(“json”,json.toString()); HttpResponse response=client.execute(请求); HttpEntity=response.getEntity(); //如果响应没有包含实体,则不需要 如果(实体!=null){ InputStream instream=entity.getContent(); //字符串结果=.convertStreamToString(流内); Log.i(“从服务器读取”、“xcx”); //Toast.makeText(上下文“yeah”,Toast.LENGTH_LONG).show(); } }捕获(可丢弃的t){ Log.i(“从服务器读取”,“XCxCxCcccccc”); //Toast.makeText(上下文,“请求失败:”+t.toString(), //Toast.LENGTH_LONG).show(); } }
我写了这段代码来发送带有参数的json对象。但是我需要知道如何发送它。我已经知道如何发送json对象了Android 发送带有参数的json对象 public void send_Json(字符串url、JSONObject Json、字符串userTken){ 试一试{ HttpParams HttpParams=新的BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams,超时0毫秒); HttpConnectionParams.setSoTimeout(httpParams,超时0毫秒); HttpClient=新的默认HttpClient(httpParams); HttpPost请求=新的HttpPost(url); List nameValuePairs=新的ArrayList(1); 添加(新的BasicNameValuePair(“userToken”,userTken)); setEntity(新的UrlEncodedFormEntity(nameValuePairs)); setEntity(新的ByteArrayEntity(json.toString().getBytes( "UTF8);; setHeader(“json”,json.toString()); HttpResponse response=client.execute(请求); HttpEntity=response.getEntity(); //如果响应没有包含实体,则不需要 如果(实体!=null){ InputStream instream=entity.getContent(); //字符串结果=.convertStreamToString(流内); Log.i(“从服务器读取”、“xcx”); //Toast.makeText(上下文“yeah”,Toast.LENGTH_LONG).show(); } }捕获(可丢弃的t){ Log.i(“从服务器读取”,“XCxCxCcccccc”); //Toast.makeText(上下文,“请求失败:”+t.toString(), //Toast.LENGTH_LONG).show(); } },android,json,Android,Json,我写了这段代码来发送带有参数的json对象。但是我需要知道如何发送它。我已经知道如何发送json对象了 public void send_Json(String url, JSONObject json,String userTken) { try { HttpParams httpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams,TIMEOUT_M
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();
}
}
代码
public void send_Json(字符串url、JSONObject Json、字符串userTken){
试一试{
HttpParams HttpParams=新的BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams,超时0毫秒);
HttpConnectionParams.setSoTimeout(httpParams,超时0毫秒);
HttpClient=新的默认HttpClient(httpParams);
HttpPost请求=新的HttpPost(url);
List nameValuePairs=新的ArrayList(1);
添加(新的BasicNameValuePair(“userToken”,userTken));
添加(新的BasicNameValuePair(“jsonKey”,json.toString());
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=client.execute(请求);
HttpEntity=response.getEntity();
//如果响应没有包含实体,则不需要
如果(实体!=null){
InputStream instream=entity.getContent();
//字符串结果=.convertStreamToString(流内);
Log.i(“从服务器读取”、“xcx”);
//Toast.makeText(上下文“yeah”,Toast.LENGTH_LONG).show();
}
}捕获(可丢弃的t){
Log.i(“从服务器读取”,“XCxCxCcccccc”);
//Toast.makeText(上下文,“请求失败:”+t.toString(),
//Toast.LENGTH_LONG).show();
}
}
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();
}
}