3 回答

TA貢獻(xiàn)1884條經(jīng)驗(yàn) 獲得超4個(gè)贊
int TIMEOUT_MILLISEC = 10000; // = 10 secondsString postMessage="{}"; //HERE_YOUR_POST_STRING.HttpParams httpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC); HttpClient client = new DefaultHttpClient(httpParams);HttpPost request = new HttpPost(serverUrl);request.setEntity(new ByteArrayEntity( postMessage.toString().getBytes("UTF8")));HttpResponse response = client.execute(request);

TA貢獻(xiàn)1966條經(jīng)驗(yàn) 獲得超4個(gè)贊
protected void sendJson(final String email, final String pwd) { Thread t = new Thread() { public void run() { Looper.prepare(); //For Preparing Message Pool for the child Thread HttpClient client = new DefaultHttpClient(); HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit HttpResponse response; JSONObject json = new JSONObject(); try { HttpPost post = new HttpPost(URL); json.put("email", email); json.put("password", pwd); StringEntity se = new StringEntity( json.toString()); se.setContentType(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 } } catch(Exception e) { e.printStackTrace(); createDialog("Error", "Cannot Estabilish Connection"); } Looper.loop(); //Loop in the message queue } }; t.start(); }

TA貢獻(xiàn)1805條經(jīng)驗(yàn) 獲得超9個(gè)贊
public void postData(String url,JSONObject obj) { // Create a new HttpClient and Post Header HttpParams myParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(myParams, 10000); HttpConnectionParams.setSoTimeout(myParams, 10000); HttpClient httpclient = new DefaultHttpClient(myParams ); String json=obj.toString(); try { HttpPost httppost = new HttpPost(url.toString()); httppost.setHeader("Content-type", "application/json"); StringEntity se = new StringEntity(obj.toString()); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); httppost.setEntity(se); HttpResponse response = httpclient.execute(httppost); String temp = EntityUtils.toString(response.getEntity()); Log.i("tag", temp); } catch (ClientProtocolException e) { } catch (IOException e) { }}
- 3 回答
- 0 關(guān)注
- 858 瀏覽
添加回答
舉報(bào)