【发布时间】:2016-10-26 12:04:25
【问题描述】:
我有一个带有文件的服务器。php 我想用我的应用程序将帖子request 发送到服务器我正在尝试这样做,但是当我在邮递员上尝试我的服务器时,我总是得到“”
{"结果": "jfYRsbW17HA3bHtaJdDm", “errorMessage”:“错误”}
我希望我的应用程序在我发送发布请求时看到那些是我的代码:(为什么我得到空值)
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();
}
}
并在另一个活动中这样调用它:
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();
【问题讨论】:
-
帮助?............?
-
可能你错过了标题或类似的东西(我也从你的 URL 收到“”)
-
在其他程序上试试这个链接它工作正常:(
-
我仍然从您的 URL 收到“”方法 = get_random_wallpaper。可能需要其他设置吗?
-
my key = method , value = get_random_wallpaper 和服务器 100% 工作,我的后端同事对其进行了测试
标签: php android http-post httpurlconnection