Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我是Android领域的新手。我想使用asyntask从php获取HTTP get reuest_Php_Android_Http_Android Asynctask - Fatal编程技术网

我是Android领域的新手。我想使用asyntask从php获取HTTP get reuest

我是Android领域的新手。我想使用asyntask从php获取HTTP get reuest,php,android,http,android-asynctask,Php,Android,Http,Android Asynctask,我只是对这个过程感到困惑。我想将TextView和Edittext1&Edittect2值传递给Second.java,,并使用httpget从login.php获取数据,并在onclick事件期间将输出提供给main类……帮助我,,,提前感谢 package com.example.messagebox; import java.io.BufferedReader; import org.apache.http.HttpResponse; import android.app.Activity

我只是对这个过程感到困惑。我想将TextView和Edittext1&Edittect2值传递给Second.java,,并使用httpget从login.php获取数据,并在onclick事件期间将输出提供给main类……帮助我,,,提前感谢

package com.example.messagebox;
import java.io.BufferedReader;
import org.apache.http.HttpResponse;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MessageBox extends Activity {
    private static final String USER_AGENT = null;
    private static String username,password,role;
    //private String link="http://192.168.1.12:81/arun/loginpost.php";
    private static String result, user , pass;
    private static HttpResponse response;
     private static BufferedReader in;
     private static StringBuffer sb;
     private EditText message,date;
     private TextView message_view,tview;
     private Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.message_box);
        message = (EditText)findViewById(R.id.message);
        date = (EditText)findViewById(R.id.date);
        message_view = (TextView)findViewById(R.id.textView_msg);
        tview = (TextView)findViewById(R.id.output_text);

        btn=(Button) findViewById(R.id.button1);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.message_box, menu);
        return true;
    }
    private void login() {

    }
}
它是Asynctask的第二个.java类

package com.example.messagebox;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URL;
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.content.Context;
import android.os.AsyncTask;
import android.widget.TextView;
public class Second extends AsyncTask<String, Void, String>{
    public Second(Context context,TextView statusField) 
    {

    }
    @Override
    protected String doInBackground(String... params) {
         try{

                String username = "arun";
                String password = "arun";
                String link = "http://192.168.1.12:81/arun/login.php?username="
                +username+"&password="+password;
                URL url = new URL(link);
                HttpClient client = new DefaultHttpClient();
                HttpGet request = new HttpGet();
                request.setURI(new URI(link));
                HttpResponse response = client.execute(request);
                BufferedReader in = new BufferedReader
               (new InputStreamReader(response.getEntity().getContent()));

               StringBuffer sb = new StringBuffer("");
               String line="";
               while ((line = in.readLine()) != null) {
                  sb.append(line);
                  break;
                }
                in.close();
                return sb.toString();

          }catch(Exception e){
             return new String("Exception: " + e.getMessage());
          }
    }
    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
    }



}
package com.example.messagebox;
导入java.io.BufferedReader;
导入java.io.InputStreamReader;
导入java.net.URI;
导入java.net.URL;
导入org.apache.http.HttpResponse;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.methods.HttpGet;
导入org.apache.http.impl.client.DefaultHttpClient;
导入android.content.Context;
导入android.os.AsyncTask;
导入android.widget.TextView;
公共类第二扩展异步任务{
公共秒(上下文、文本视图状态字段)
{
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
试一试{
字符串username=“arun”;
字符串password=“arun”;
字符串链接=”http://192.168.1.12:81/arun/login.php?username="
+用户名+“&password=“+password;
URL=新的URL(链接);
HttpClient=new DefaultHttpClient();
HttpGet请求=新建HttpGet();
setURI(新URI(link));
HttpResponse response=client.execute(请求);
BufferedReader in=新的BufferedReader
(新的InputStreamReader(response.getEntity().getContent());
StringBuffer sb=新的StringBuffer(“”);
字符串行=”;
而((line=in.readLine())!=null){
某人附加(行);
打破
}
in.close();
使某人返回字符串();
}捕获(例外e){
返回新字符串(“异常:+e.getMessage());
}
}
@凌驾
受保护的void onPostExecute(字符串结果){
//TODO自动生成的方法存根
super.onPostExecute(结果);
}
}
这是我的xml


也许这段代码会对你有所帮助

public class MessageBox extends Activity {
private static final String USER_AGENT = null;
private static String username,password,role;
//private String link="http://192.168.1.12:81/arun/loginpost.php";
private static String result, user , pass;
private static HttpResponse response;
 private static BufferedReader in;
 private static StringBuffer sb;
 private EditText message,date;
 private TextView message_view,tview;
 private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.message_box);
    message = (EditText)findViewById(R.id.message);
    date = (EditText)findViewById(R.id.date);
    message_view = (TextView)findViewById(R.id.textView_msg);
    tview = (TextView)findViewById(R.id.output_text);

    btn=(Button) findViewById(R.id.button1);
    btn.setOnClickListner((OnClickListener) this);

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.message_box, menu);
    return true;
}

private void onClick(View v) {
new sendPostData().execute();
}

 private class sendPostData extends AsyncTask<String, Void, String>
{
        @Override
    protected String doInBackground(String... params) {

String line="";

 try{

            String username = "arun";
            String password = "arun";
            String link = "http://192.168.1.12:81/arun/login.php?username="
            +username+"&password="+password;
            URL url = new URL(link);
            HttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet();
            request.setURI(new URI(link));
            HttpResponse response = client.execute(request);
            BufferedReader in = new BufferedReader
           (new InputStreamReader(response.getEntity().getContent()));

           StringBuffer sb = new StringBuffer("");

           while ((line = in.readLine()) != null) {
              sb.append(line);
              break;
            }
            in.close();
            return sb.toString();

      }catch(Exception e){
         return new String("Exception: " + e.getMessage());
      }

    }
       @Override
    protected void onPostExecute(String result) {
        //View your result here.
            message_view.setText(line);
        }
 }

   }
公共类MessageBox扩展活动{
私有静态最终字符串USER\u AGENT=null;
私有静态字符串用户名、密码、角色;
//专用字符串链接=”http://192.168.1.12:81/arun/loginpost.php";
私有静态字符串结果、用户、传递;
专用静态HttpResponse响应;
中的专用静态缓冲读取器;
私人静态缓冲区sb;
私人编辑短信,日期;
私有文本视图消息_视图,tview;
专用按钮btn;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.message_框);
message=(EditText)findViewById(R.id.message);
日期=(EditText)findViewById(R.id.date);
message_view=(TextView)findViewById(R.id.TextView_msg);
tview=(TextView)findViewById(R.id.output_text);
btn=(按钮)findViewById(R.id.button1);
setOnClickListner((OnClickListener)this);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(右菜单消息框,菜单);
返回true;
}
私有void onClick(视图v){
新建sendPostData().execute();
}
私有类sendPostData扩展异步任务
{
@凌驾
受保护的字符串doInBackground(字符串…参数){
字符串行=”;
试一试{
字符串username=“arun”;
字符串password=“arun”;
字符串链接=”http://192.168.1.12:81/arun/login.php?username="
+用户名+“&password=“+password;
URL=新的URL(链接);
HttpClient=new DefaultHttpClient();
HttpGet请求=新建HttpGet();
setURI(新URI(link));
HttpResponse response=client.execute(请求);
BufferedReader in=新的BufferedReader
(新的InputStreamReader(response.getEntity().getContent());
StringBuffer sb=新的StringBuffer(“”);
而((line=in.readLine())!=null){
某人附加(行);
打破
}
in.close();
使某人返回字符串();
}捕获(例外e){
返回新字符串(“异常:+e.getMessage());
}
}
@凌驾
受保护的void onPostExecute(字符串结果){
//在这里查看您的结果。
message_view.setText(行);
}
}
}
不要忘记在Manifest.xml文件中添加权限

<uses-permission android:name="android.permission.INTERNET" />


不要将AsyncTask作为单独的类使用。在MainActivity中使用该类我也尝试过,但它显示android.os.networkexception错误,因为您没有在AndroidManifest.xml文件中添加INTERNET权限。发布AndroidManifest.xml代码。它在btn.setOnClickListener中显示错误(此);类型视图中的方法setOnClickListener(View.OnClickListener)不适用于此listner的参数(MessageBox)add-cast参数。像btn.setOnClickListener((OnClickListener)this);通过使用btn.setOnClickListener((OnClickListener)this);我的活动很遗憾,我已经编辑了答案,现在试试这个。您听说过调试器吗?使用调试器并检查是否存在问题。导致应用程序停止的点。
<uses-permission android:name="android.permission.INTERNET" />