- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在关注这个网站 http://givemepass.blogspot.hk/2011/12/http-server.html尝试使用 android 应用程序连接 PHP 服务器以获取消息。
GetServerMessage.java
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class GetServerMessage {
public String stringQuery(String url){
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost method = new HttpPost(url);
HttpResponse response = httpclient.execute(method);
HttpEntity entity = response.getEntity();
if(entity != null){
return EntityUtils.toString(entity);
}
else{
return "No string.";
}
}
catch(Exception e){
return "Network problem";
}
}
}
GetPhpServerMessageDemoActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class GetPhpServerMessageDemoActivity extends Activity {
/** Called when the activity is first created. */
private TextView textView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView)findViewById(R.id.text_view);
GetServerMessage message = new GetServerMessage();
String msg = message.stringQuery("http://192.168.1.88/androidtesting.php");
textView.setText("Server message is "+msg);
}
}
我试图从该站点下载 Android 应用程序项目 http://uploadingit.com/file/d4632ekfpwjiupyn/GetPhpServerMessageDemo2.zip在我的手机上运行,它的工作。但是当我开始一个新项目(Minimuim Requied SDK:API8,目标 SDK:API17,编译:API17)并复制这两个 java 代码时。我添加了权限android.permission.INTERNET
,所以我不知道问题在哪里,我只知道当运行HttpResponse response = httpclient.execute(method);
出现错误并返回字符串“网络问题”。
最佳答案
更新:如果您使用的是 kotlin,请使用协程进行线程处理。 asnctask 不再使用。
您正在 ui 线程上运行与网络相关的操作。你将得到 NetworkOnMainThreadException
post honeycomb。
使用Thread
或Asynctask
调用异步任务
new TheTask().execute("http://192.168.1.88/androidtesting.php");
异步任务
http://developer.android.com/reference/android/os/AsyncTask.html
class TheTask extends AsyncTask<String,String,String>
{
@Override
protected String onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
// update textview here
textView.setText("Server message is "+result);
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost method = new HttpPost(params[0]);
HttpResponse response = httpclient.execute(method);
HttpEntity entity = response.getEntity();
if(entity != null){
return EntityUtils.toString(entity);
}
else{
return "No string.";
}
}
catch(Exception e){
return "Network problem";
}
}
}
更新 HttpClient 在 api 23 中被弃用。使用 HttpUrlConnection
。
http://developer.android.com/reference/java/net/HttpURLConnection.html
关于java - Android 4.2 错误上的 HttpClient.execute(HttpPost),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19162272/
我不明白为什么需要使用 [HttpPost("[action]")] 而不是 [HttpPost]? 如果使用[HttpPost("[action]")],那么来自角度客户端的请求会触发 Contro
我不明白为什么需要使用 [HttpPost("[action]")] 而不是 [HttpPost]? 如果使用[HttpPost("[action]")],那么来自角度客户端的请求会触发 Contro
所以我们有[HttpPost],这是一个可选属性。我知道这会限制调用,因此只能通过 HTTP POST 请求进行调用。我的问题是我为什么要这样做? 最佳答案 想象一下以下情况: [HttpGet] p
在 Scott Hanselman 的书中(第 1 章),他为我们提供了两个选项来实现 Create 操作方法的 [HttpPost]。 第一个依赖于 TryUpdateModel 根据传入的表单字段
我正在从 Android 应用程序向服务器发送 JSON 数据,我在人们添加的示例中看到: httpPost.setHeader("Content-type", "application/json")
- - - 更新 能够通过使用 UsernamePasswordCredentials 类来修复它 代码如下所示 val 客户端 = 新的 DefaultHttpClient client.getCr
这个问题让我很恼火。我正在重复使用适用于其他项目/类但不适用于我当前项目的相同代码。我所做的只是更改变量(POST 值和 EditText 名称)。但没有成功。 import java.util.Ar
这是我的代码: public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
我正在为黑莓开发应用程序,我需要向我的服务器发送一个 Http 发布请求。我正在使用模拟器来测试我的应用程序,我发现这段代码是为了发送请求: http://vasudevkamath.techfiz.
我希望能够使用参数查看完整的 HttpClient/HttpPost URI。我不确定如何将其输出到我的控制台。 HttpClient httpclient = HttpClients.createD
我有一个带有 httpPost 属性的 ActionResult。如果我没有记错的话,当用户从 DateTimePicker 中选择日期时,我需要此属性。就我而言,我有 2 个日期时间选择器,类型为
嗨:)我遇到了一个简单的问题,但非常烦人。我正在尝试使用 HttpPost 类发送 http post 请求 这是返回InputStream的方法的一部分: HttpClient
我正在尝试通过 android studio 中的 java 将发布请求发送到我的 PHP 页面 首先我尝试了 httppost,但按下该键后应用程序崩溃了 然后我读到我必须使用新线程,我这样做了,但
我正在尝试将我的应用程序设置为在 HttpPost 上 3 秒后抛出超时异常。我正在从 ASyncTask 执行此请求。出于某种原因,即使我给它一个不存在的域,它也会挂起大约一两分钟,然后抛出最后一个
我正在尝试从 Android 应用程序中向我的 Google App Engine 应用程序发送 POST 请求,以便将文件上传到 blobstore 并将一些元数据上传到数据存储区。我构建了一个可以
在我的 CountryActivity.java 中,我有一个 HttpRequest 来检索维基百科的 json 信息。这是我使用 AsyncTask 的代码: private class D
我的 MainAcitivity 中有以下代码,用于使用方法 jsnrqstr.sendRequest(username, password, URL); 获取的用户名和密码登录到应用程序/p> lo
我有一个网页用 1 (true) 或 0 (false) 响应日志记录请求。当我尝试用另一个网页调用请愿书时,结果是正确的,它可以是 0 或 1。但是当我用 Android 应用程序调用它时,结果总是
在我的应用程序中,我必须从 4 个不同的 url 获取数据,然后在获取数据完成后,我必须按特定顺序显示项目。我正在使用 HttoPost 发送发布请求。我在不同的线程中分别发送 4 个请求。当线程获取
我尝试了所有可能的方法,但仍然出现以下错误 -1.无法解析符号ClientProtocolException2.无法解析符号HttpPost3.无法解析符号DefaultHttpClient JSON
我是一名优秀的程序员,十分优秀!