我想从 android 应用程序从我的服务器上的 php 文件中检索数据。我已经搜索了 2 天的工作示例,但我能找到的只是:
http://www.androidaspect.com/2013/05/how-to-connect-android-with-php.html http://fahmirahman.wordpress.com/2011/04/21/connection-between-php-server -and-android-client-using-http-and-json/
我也看过很多关于 SO 的类似问题,但我试图重新创建这个问题是徒劳的。因此,如果您认为我的问题重复,请原谅。
我基本上想在这样的 php 脚本上检索回显的 php 数据:
<?php
echo "Hello PHP";
?>
并将其显示在 TextView 中。
这是我的名为 Home_Activity.java 的主要活动代码
package com.example.httppost;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView textView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TextView to display result
TextView textView = (TextView) findViewById(R.id.text_display);
new GetData(textView).execute("");
}
private class GetData extends AsyncTask<String, Void, String> {
private TextView display;
GetData(TextView view){
this.display = view;
}
protected String doInBackground(String... message) {
HttpClient httpclient;
HttpGet request;
HttpResponse response = null;
String result = "error";
// TextView to display result
// Try to connect using Apache HttpClient Library
try {
httpclient = new DefaultHttpClient();
request = new HttpGet("http://10.0.2.2:8080/food.php");
response = httpclient.execute(request);
}
catch (Exception e) {
// Code to handle exception
result = "error";
}
// response code
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
// Appending result to textview
result = result + line ;
}
} catch (Exception e) {
// Code to handle exception
result = "error";
}
return result;
}
protected void onPostExecute(String result) {
this.display.setText(result);
}
}}
错误日志看起来像象形文字。该程序短暂执行,然后我得到可怕的“不幸的是 home_automation 已停止”。我认为这与我尝试在异步任务中设置 textView 有关,但我不知道如何解决这个问题,也不知道我发送 http 请求的代码是否正确。我所知道的是 php 和服务器的东西是正确的。所有教程都展示了如何在主线程中执行此操作。当我这样做时,我不断在主要胎面错误消息上获得网络。这是我第一个使用 http 请求和异步任务的应用程序。我是边线精神错乱。请帮我告诉我哪里出错了,提前谢谢!
编辑:这是我的堆栈跟踪:
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.httppost.MainActivity$GetData.onPostExecute(MainActivity.java:69)
at com.example.httppost.MainActivity$GetData.onPostExecute(MainActivity.java:1)
at android.os.AsyncTask.finish(AsyncTask.java:631)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
我 99% 确定问题出在 textView.setText(result);
我编辑了我的代码但我仍然收到 NullPointer 异常