2013-08-02 2 views
0

Im пытается выполнить следующий запрос в моем приложении для Android. Запрос, который я отправил, был в браузере Chrome. В запросе я сокращаю данные формы, поэтому кода не так много.Post Request clone на Android

Request URL:http://***/ 
Request Method:POST 
Status Code:200 OK 

Request Headers 
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
Accept-Encoding:gzip,deflate,sdch 
Accept-Language:de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4 
Cache-Control:max-age=0 
Connection:keep-alive 
Content-Length:424 
Content-Type:application/x-www-form-urlencoded 
Cookie:__utma=188893489.1646114392.1358703936.1367178892.1368783485.29; __utmz=188893489.1365594840.21.3.utmcsr=***|utmccn=(referral)|utmcmd=referral|utmcct=/ 
Host:*** 
Origin:*** 
Referer:** 
User-Agent:Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36 

Form Data 
date=1375480155&mail=&dfBoot=Test 

Response Headers 
Connection:Keep-Alive 
Content-Encoding:gzip 
Content-Length:3614 
Content-Type:text/html 
Date:Fri, 02 Aug 2013 21:49:59 GMT 
Keep-Alive:timeout=15, max=100 
Server:Apache/2.2.14 (Ubuntu) 
Vary:Accept-Encoding 
X-Powered-By:PHP/5.3.2-1ubuntu4.20 

с этим кодом:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() 
       .permitAll().build(); 
     StrictMode.setThreadPolicy(policy); 

     URL url = new URL("http://***/index.php"); 
     HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
     try { 
      connection.setRequestMethod("POST"); 
      connection.setDoInput(true); 
      connection.setDoOutput(true); 
      connection.setUseCaches(false); 
      connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); 
      connection.setRequestProperty("Content-Length",String.valueOf(post.length())); 
      //connection.setChunkedStreamingMode(0);//results in frezing 
      OutputStreamWriter writer = new OutputStreamWriter(
        connection.getOutputStream()); 
      writer.write(post); 
      writer.flush(); 
      InputStream in = new BufferedInputStream(
        connection.getInputStream()); 
      while (in.available() != 0) { 
       in.read(); 
      } 
      writer.close(); 
      in.close(); 
     } finally { 
      connection.disconnect(); 
     } 

код очень перепутались, потому что я судимое manytime исправить мою проблему подключения. Пожалуйста, помогите с лучшим решением.

ответ

0

Хм, я покажу свой рабочий код с JSON, но, возможно, вы можете его изменить.

URL url = new URL("http://xcxcxcxcxcx"); 
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
      connection.setRequestProperty("Content-Type", "application/json"); 
      connection.setRequestProperty("charset", "utf-8"); 
      connection.setRequestProperty("Content-Length", "" + Integer.toString(jsonObject.toString().getBytes().length)); 
      connection.setRequestMethod("POST"); 
      connection.setDoOutput(true); 
      connection.connect(); 
      DataOutputStream out = new DataOutputStream(connection.getOutputStream()); 
      out.writeBytes(jsonObject.toString()); 
      out.flush(); 
      out.close(); 
      InputStream is = connection.getInputStream(); 
      InputStreamReader reader = new InputStreamReader(is); 
      BufferedReader r = new BufferedReader(reader); 
       StringBuilder total = new StringBuilder(); 
       String line; 
       while ((line = r.readLine()) != null) { 
        total.append(line); 
       } 

       JSONObject jsonObjectResult = null; 
       if(typeOfMethod == 0){ 
       JSONTokener tokenizer = new JSONTokener(total.toString()); 
       jsonObjectResult = new JSONObject(tokenizer); 
       } 
      connection.disconnect(); 
      return jsonObjectResult; 

Есть некоторые различия между получением результата от InputStream.

+0

Сервер по-прежнему не принимает сообщение. – BattleFrogg