Android Httppost
Hi i am using below code to send data to server
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.myurl.com/app/page.php");
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
nameValuePairs.add(new BasicNameValuePair("name", "dave"));
nameValuePairs.add(new BasicNameValuePair("taxi no", "354"));
nameValuePairs.add(new BasicNameValuePair("pack", "0"));
nameValuePairs.add(new BasicNameValuePair("exchk", "1"));
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
try {
HttpResponse response = httpclient.execute(httppost);
String responseBody = 开发者_如何转开发EntityUtils.toString(response.getEntity());
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
I can successfully send data to server,but the server return larger amount of data(100Kb-200KB) in text format.I want to convert that text response into json object.so i am assigning all the response into one string to convert json object.
But That Response string contain only less amount of data.
I checked the server it send 112kb file but that response String contain only less data.say around (50kb-75kb).
So can u please any one guide me to solve the problem
ArrayList is an expandable List item. So I have no idea what the purpose of using "5" as an argument. Second thing is BasicNameValuePair contains names and values as arguments. It is a good practice to use single word for a name attribute. Remove
String responseBody = EntityUtils.toString(response.getEntity());
Add following code sample
try {
HttpResponse httpResponse = httpClient.execute(httpPost);
InputStream inputStream = httpResponse.getEntity().getContent();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder stringBuilder = new StringBuilder();
String bufferedStrChunk = null;
while((bufferedStrChunk = bufferedReader.readLine()) != null){
stringBuilder.append(bufferedStrChunk);
}
return stringBuilder.toString();
} catch (ClientProtocolException cpe) {
System.out.println("First Exception caz of HttpResponese :" + cpe);
cpe.printStackTrace();
} catch (IOException ioe) {
System.out.println("Second Exception caz of HttpResponse :" + ioe);
ioe.printStackTrace();
}
This return the page content in a readable format. Then you can pass it to a JSON file by using JSONObject and JSONArray.
精彩评论