发布请求可能会触发

时间:2015-04-09 12:47:38

标签: java android

我正在开发一个应用程序,其中我通过HTTP POST请求将数据发送到PHP服务器。但是,问题是邮政要求交替发生; (例如;第一次,它将数据发送到服务器。第二次,它没有。这继续。)

这是我的代码:

HttpURLConnection conn = null;
        try {
            conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            conn.setFixedLengthStreamingMode(bytes.length);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded;charset=UTF-8");
            // post the request
            OutputStream out = conn.getOutputStream();
            out.write(bytes);
            out.close();
            // handle the response
            int status = conn.getResponseCode();
            Log.e("status code",String.valueOf(status));
            if (status != 200) {
                Log.e("ServerUtilities","post failed . status 200");
                throw new IOException("Post failed with error code " + status);
            }
        }
        catch(Exception e)
        {
           Log.e("Exception",e.getMessage());
        }
        finally{
            if (conn != null) {
                InputStream response = conn.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(response));
                String line;
                String serverResponseMessage="";
                while ((line = reader.readLine()) != null) {
                    serverResponseMessage += line;
                }
                Log.e("serverResponse",serverResponseMessage);
                reader.close();
                response.close();
                conn.disconnect();
            }

1 个答案:

答案 0 :(得分:2)

请按照以下代码进行操作。它对我很好。

public class ServiceHandler
{

  static String response = null;
  public final static int GET = 1;
  public final static int POST = 2;

  public ServiceHandler()
  {

  }


  public String makeServiceCall(String url, int method)
  {
    return this.makeServiceCall(url, method, null);
  }


  public String makeServiceCall(String url, int method, List<NameValuePair> params) {
    try {

      DefaultHttpClient httpClient = new DefaultHttpClient();
      HttpEntity httpEntity = null;
      HttpResponse httpResponse = null;

        if (method == POST) {
          HttpPost httpPost = new HttpPost(url);

          if (params != null) {
            httpPost.setEntity(new UrlEncodedFormEntity(params));
          }

          Log.d("before httpclient","execute") ;

          httpResponse = httpClient.execute(httpPost);
          Log.d("after httpclient", "execute") ;
        } else if (method == GET) {

            if (params != null) {
              String paramString = URLEncodedUtils.format(params, "utf-8");
              url += "?" + paramString;
            }

            HttpGet httpGet = new HttpGet(url);
            httpResponse = httpClient.execute(httpGet);

        }

        httpEntity = httpResponse.getEntity();

        response = EntityUtils.toString(httpEntity);

        Log.d("response", response) ;

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return response;

  }
}