2014-09-17 3 views
0

Чтобы отправить данные на сервер, я использую следующий метод: успешно отправляет сообщения на сервер, здесь мне нужно получить один номер, который является идентификатором регистрации. Я закодировал это на сервере php. Я старался, чтобы почтальон прекрасно работал, но здесь я не получаю. Поэтому, пожалуйста, дайте мне знать, что я должен делать?Получить ответ после отправки на сервер

static String result=null;  
    private static void post(String endpoint, Map<String, String> params) 
      throws IOException { 
     URL url; 
     try { 
      url = new URL(endpoint); 
     } catch (MalformedURLException e) { 
      throw new IllegalArgumentException("invalid url: " + endpoint); 
     } 
     StringBuilder bodyBuilder = new StringBuilder(); 
     Iterator<Entry<String, String>> iterator = params.entrySet().iterator(); 
     // constructs the POST body using the parameters 
     while (iterator.hasNext()) { 
      Entry<String, String> param = iterator.next(); 
      bodyBuilder.append(param.getKey()).append('=') 
        .append(param.getValue()); 
      if (iterator.hasNext()) { 
       bodyBuilder.append('&'); 
      } 
     } 
     String body = bodyBuilder.toString(); 
     Log.v("TWA CLIENT REPORT", "Posting '" + body + "' to " + url); 
     byte[] bytes = body.getBytes(); 
     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(); 
      if (status != 200) { 
       throw new IOException("Post failed with error code " + status); 
      } 
     } finally { 
      if (conn != null) { 
       conn.disconnect(); 
      } 
     } 

     } 

ответ

0

попробовать это фрагменты, я буду работать с вами,

URL url; 
url = new URL("http://www.url.com/app.php"); 
URLConnection connection; 
connection = url.openConnection(); 
HttpURLConnection httppost = (HttpURLConnection) connection; 
httppost.setDoInput(true); 
httppost.setDoOutput(true); 
httppost.setRequestMethod("POST"); 
httppost.setRequestProperty("User-Agent", "Tranz-Version-t1.914"); 
httppost.setRequestProperty("Accept_Language", "en-US"); 
httppost.setRequestProperty("Content-Type", 
     "application/x-www-form-urlencoded"); 
DataOutputStream dos = new DataOutputStream(httppost.getOutputStream()); 
dos.write(b); // bytes[] b of post data 

String reply; 
InputStream in = httppost.getInputStream(); 
StringBuffer sb = new StringBuffer(); 
try { 
    int chr; 
    while ((chr = in.read()) != -1) { 
     sb.append((char) chr); 
    } 
    reply = sb.toString(); 
} finally { 
    in.close(); 
} 
+0

Спасибо Я могу получить регистрационный код с сервера – user1799171

Смежные вопросы