2013-08-02 6 views
0

내 안드로이드 애플 리케이션에서 folowing 요청을 수행하려고하는 임. 보낸 요청은 Chrome 브라우저에서 발생했습니다. 요청에서 양식 데이터를 줄여서 코드가 너무 많지 않습니다. 이 코드게시물 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 

: 난 내 연결 문제를 해결하기 위해 manytime을 tryed 때문에

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(); 
     } 

코드는 매우 엉망이된다. 더 나은 솔루션을 찾으십시오.

답변

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