2016-10-26 158 views
0

我有一个文件的服务器。 php好,我想送岗位request服务器与我的应用我想这样做,但我总是得到“”当我试穿邮递员我的服务器我得到HttpurlConnection Post请求正文php服务器?

{“结果”:“jfYRsbW17HA3bHtaJdDm” , “的errorMessage”:“错误”}

我想我的应用程序看到这些时,我在这里发送POST请求我的代码:(为什么我得到空)

public class HttpParoonakRequest { 


public static String HttpParoonakRequestGetUrl(){ 


    URL url ; 
     HttpURLConnection client = null; 
    try { 

     url = new URL("myphp"); 

     client = (HttpURLConnection) url.openConnection(); 
     client.setRequestMethod("POST"); 

     client.setRequestProperty("method","get_random_wallpaper"); 



     client.setDoInput(true); 
     client.setDoOutput(true); 
     client.connect(); 
     DataOutputStream wr = new DataOutputStream (
       client.getOutputStream()); 

     wr.flush(); 
     wr.close(); 

     //Get Response 
     InputStream is = client.getInputStream(); 
     BufferedReader rd = new BufferedReader(new InputStreamReader(is)); 
     String line; 
     StringBuffer response = new StringBuffer(); 

     while((line = rd.readLine()) != null) { 
      response.append(line); 
      response.append('\n'); 

     } 
     Log.d("hey",response.toString()); 

     rd.close(); 

     return response.toString(); 


    } catch (Exception e) { 

     e.printStackTrace(); 
     return e.getMessage(); 
    } 
    finally { 
     if(client != null) // Make sure the connection is not null. 
      client.disconnect(); 
    } 



} 

,并调用它这种方式在另一个acti VITY:

thread = new Thread(new Runnable() { 


      @Override 
      public void run() { 
       try { 
        String abcd = HttpParoonakRequest.HttpParoonakRequestGetUrl(); 
        System.out.println("Message1 "+ abcd); 

       } catch (Exception e) { 

        e.printStackTrace(); 
        e.getMessage(); 
       } 

      } 
     }); 
     thread.start(); 
+0

帮助....... ..? –

+0

也许你会错过标题或类似的东西(我也从你的网址收到“”) –

+0

尝试它在其他程序这个链接它工作正常:( –

回答

0

你路过走错了路参数:你应该client.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");取代client.setRequestProperty("method","get_random_wallpaper"),并通过wr流这样的传递请求体的参数:

   ...  

       client = (HttpURLConnection) url.openConnection(); 
       client.setRequestMethod("POST"); 

       client.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 

       client.setDoInput(true); 
       client.setDoOutput(true); 
       client.connect(); 
       DataOutputStream wr = new DataOutputStream(
         client.getOutputStream()); 

       wr.write("method=get_random_wallpaper".getBytes()); 

       wr.flush(); 
       wr.close(); 
       ... 
+0

非常感谢你..它的工作 –

+0

我可以收到这样的数据可以说像这样的图像??再次感谢你 –

+0

欢迎您! ,任何数据:图片,音频文件等 –