如何使用异步任务连接到安卓中的php


How to connect to php in android using an async task?

我想从安卓应用程序中从服务器上的 php 文件中检索数据。我已经在寻找一个工作示例 2 天了,我能找到的就是这个:

http://www.androidaspect.com/2013/05/how-to-connect-android-with-php.htmlhttp://fahmirahman.wordpress.com/2011/04/21/connection-between-php-server-and-android-client-using-http-and-json/

我也看过很多关于SO的类似问题,但我试图重新创建这个问题是徒劳的。因此,如果您认为我的问题重复,请原谅我。

我基本上想在 php 脚本上检索回显的 php 数据,如下所示:

<?php
echo "Hello PHP";
?>

并将其显示在文本视图中。

这是我的主要活动代码,名为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);我已经编辑了我的代码,但我仍然收到空指针异常

对不起,伙计们,我觉得自己是个菜鸟。一定是不小心删除了

setContentView(R.layout.activity_main);

这就解释了应用程序过早停止的原因。我也忘了在我的异步类中实例化 TextView,它解释了空指针。谢谢大家的评论和帮助!