我通过网址将信息发送到php文件。当我在浏览器中键入url时,会显示正确的信息,但是我没有通过InputStreamReader获取此信息。
这是我的代码。
try {
URL url = new URL("http://example/example.php?userImage="
+ URLEncoder.encode(userImageString, "UTF-8") + "&userHeight="
+ URLEncoder.encode(userHeight,"UTF-8" ));
Log.d("url", "" + url);
Scanner in = new Scanner(new InputStreamReader(url.openStream()))
.useDelimiter("\n");
Log.d("hasnext?", "" + in.hasNext());
while (in.hasNext())
{
Log.d("sent", in.next());
}
in.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
HasNext返回false,它应该返回true。有什么建议吗?
我应该注意userImageString是一个Base64编码的字符串。
答案 0 :(得分:1)
1)您是否有权访问互联网?
<uses-permission android:name="android.permission.INTERNET" />
2)我认为你可以用这个答案来解决你的问题: Make an HTTP request with android
3)这是一个有效的http请求,它返回响应:
public String request(String fUrl){
String page = "";
try
{
HttpClient hc = new DefaultHttpClient();
HttpPost post = new HttpPost(fUrl);
HttpResponse response = hc.execute(post);
if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
{
page = EntityUtils.toString(response.getEntity());
}
} catch(IOException e) {
e.printStackTrace();
System.out.println("Exception Thrown");
}
return page;
}