2014-04-22 57 views
0

我是triyng發送POST請求到服務器。以下Java代碼適用於PC應用程序,但不適用於我的Android應用程序。添加了Iternet權限,那麼,我需要做些什麼來發送這篇文章,以及我必須使用哪些其他方法和庫來替代Android的這些代碼?從Android應用程序發送POST

 String hostname = "xxxxxxx.box"; 
     int port = 80; 
     InetAddress addr = InetAddress.getByName(hostname); 
     Socket sock = new Socket(addr, port); 
     String SID = new classSID("xxxxxx").obtainSID(); 
     BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream(),"UTF-8")); 
     String str = "enabled=on&username="+givenName+"&email="+givenEmail+"&password="+givenPassword+"&frominternet=on&box_admin_rights=on&phone_rights=on&homeauto_rights=on&uid=&sid="+SID+"&apply="; 

     //////////////////////////////////////////////////////////////////////////////////////////// 
     wr.write("POST /system/boxuser_edit.lua HTTP/1.1"); 
     wr.write("Host: xxxxxx:80" + "\r\n"); 
     wr.write("Accept: text/html" + "\r\n"); 
     wr.write("Keep-Alive: 300" + "\r\n"); 
     wr.write("Connection: Keep-Alive" + "\r\n"); 
     wr.write("Content-Type: application/x-www-form-urlencoded"+"\r\n"); 
     wr.write("Content-Length: "+str.length()+"\r\n"); 
     wr.write("\r\n"); 
     wr.write(str+"\r\n"); 
     wr.flush(); 
    ////////////////////////////////////////////////////////////////////////////////////////////   
     BufferedReader rd = new BufferedReader(new InputStreamReader(sock.getInputStream(),"UTF-8")); 
     String line; 
     while((line = rd.readLine()) != null) 
      Log.v("Response", line);  

     wr.close(); 
     rd.close(); 
     sock.close(); 
} 
+0

什麼是錯誤 –

+0

您好,我試圖捕獲錯誤,但錯誤不存在。只是不行。 – Soyer

+0

考慮使用IP而不是主機名,並使用我在 –

回答

1

試試這個:和考慮使用IP的主機名,而不是因爲你的Android設備,可能n要能夠解決本地主機名

private static final String  UTF_8  = "UTF-8"; 
/** 
* @param hostNameOrIP 
*   : the host name or IP<br/> 
* @param webService 
*   : the web service name<br/> 
* @param classOrEndPoint 
*   : the file or end point<br/> 
* @param method 
*   : the method being called<br/> 
* @param parameters 
*   : the parameters to be sent in the message body 
* @return 
*/ 
public static String connectPOST(final String url, final HashMap<String, String> parameters) { 
    final StringBuilder postDataBuilder = new StringBuilder(); 
    if (null != parameters) { 
     for (final HashMap.Entry<String, String> entry : parameters.entrySet()) { 
      if (postDataBuilder.length() != 0) { 
       postDataBuilder.append("&"); 
      } 
      postDataBuilder.append(entry.getKey()).append("=").append(entry.getValue()); 
     } 
    } 

    final StringBuffer text = new StringBuffer(); 
    HttpURLConnection conn = null; 
    OutputStream out = null; 
    InputStreamReader in = null; 
    BufferedReader buff = null; 
    try { 
     final URL page = new URL(url); 
     conn = (HttpURLConnection) page.openConnection(); 
     conn.setDoInput(true); 
     conn.setDoOutput(true); 
     conn.setUseCaches(false); 
     conn.setRequestMethod("POST"); 
     out = conn.getOutputStream(); 
     final byte[] postData = postDataBuilder.toString().getBytes(UTF_8); 
     out.write(postData); 
     out.flush(); 
     out.close(); 
     final int responseCode = conn.getResponseCode(); 
     if ((responseCode == 401) || (responseCode == 403)) { 
      // Authorization Error 
      Log.e(TAG, "Authorization error in " + url + "(" + postDataBuilder.toString() + ")"); 
      // throw new Exception("Authorization Error in " + method + "(" 
      // + postDataBuilder.toString() + ")"); 
      return null; 
     } 
     if (responseCode == 404) { 
      // Authorization Error 
      Log.e(TAG, "Not found error in " + url + "(" + postDataBuilder.toString() + ")"); 
      // throw new Exception("Authorization Error in " + method + "(" 
      // + postDataBuilder.toString() + ")"); 
      return null; 
     } 

     if ((responseCode >= 500) && (responseCode <= 504)) { 
      // Server Error 
      Log.e(TAG, "Internal server error in " + url + "(" + postDataBuilder.toString() + ")"); 
      // throw new Exception("Internal Server Error in " + method + 
      // "(" 
      // + postDataBuilder.toString() + ")"); 
      return null; 
     } 
     in = new InputStreamReader((InputStream) conn.getContent()); 
     buff = new BufferedReader(in); 
     String line; 
     while ((null != (line = buff.readLine())) && !"null".equals(line)) { 
      text.append(line + "\n"); 
     } 
     buff.close(); 
     buff = null; 
     in.close(); 
     in = null; 
     conn.disconnect(); 
     conn = null; 
    } catch (final Exception e) { 
     Log.e(TAG, "Exception while connecting to " + url + " with parameters: " + postDataBuilder + ", exception: " + e.toString() + ", cause: " 
       + e.getCause() + ", message: " + e.getMessage()); 
     e.printStackTrace(); 
     return null; 
    } finally { 
     if (null != out) { 
      try { 
       out.close(); 
      } catch (final IOException e1) { 
      } 
      out = null; 
     } 
     if (null != buff) { 
      try { 
       buff.close(); 
      } catch (final IOException e1) { 
      } 
      buff = null; 
     } 
     if (null != in) { 
      try { 
       in.close(); 
      } catch (final IOException e1) { 
      } 
      in = null; 
     } 
     if (null != conn) { 
      conn.disconnect(); 
      conn = null; 
     } 
    } 
    final String temp = text.toString(); 
    if (text.length() > 0) { 
     Log.i(TAG, "Success in " + url + "(" + postDataBuilder.toString() + ") = " + temp); 
     return temp; 
    } 
    Log.w(TAG, "Warning: " + url + "(" + postDataBuilder.toString() + "), text = " + temp); 
    return null; 
} 
+0

下提供的代碼進行測試。你不需要指定端口80 –

+0

你好Shereef,你能告訴我請求類通信來自何處,爲什麼(InputStream)拋出錯誤?萬分感謝! – Soyer

+0

對不起,通訊班是我的班級,我刪除了所有對它的引用 –

1

的問題是與端口80上的你需要創建套接字root權限訪問端口< 1024在android中。請參閱issue

相關問題