简体   繁体   English

Android

[英]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. 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).我检查了它发送 112kb 文件的服务器,但响应字符串只包含较少的 data.say 大约(50kb-75kb)。

So can u please any one guide me to solve the problem所以你能请任何人指导我解决问题吗

ArrayList is an expandable List item. ArrayList 是一个可扩展的列表项。 So I have no idea what the purpose of using "5" as an argument.所以我不知道使用“5”作为参数的目的是什么。 Second thing is BasicNameValuePair contains names and values as arguments.第二件事是 BasicNameValuePair 包含名称和值,如 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.然后,您可以使用 JSONObject 和 JSONArray 将其传递给 JSON 文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM