【问题标题】:Am new to Android field.I want to get HTTP get reuest from php,,using asyntask我是 Android 领域的新手。我想从 php 获取 HTTP 获取请求,使用 asynctask
【发布时间】:2014-02-21 03:55:47
【问题描述】:

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

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() {

    }
}

这是我的第二个.java Asynctask 类

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);
    }



}

这是我的xml

   <EditText
      android:id="@+id/editText2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignRight="@+id/editText1"
      android:layout_below="@+id/editText1"
      android:layout_marginTop="25dp"
      android:ems="10"
      android:inputType="textPassword" >
   </EditText>

   <EditText
      android:id="@+id/editText1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentRight="true"
      android:layout_alignParentTop="true"
      android:layout_marginTop="44dp"
      android:ems="10" >

   <requestFocus android:layout_width="wrap_content" />

   </EditText>

   <TextView
      android:id="@+id/textView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignBottom="@+id/editText1"
      android:layout_alignParentLeft="true"
      android:text="@string/Username" />

   <TextView
      android:id="@+id/textView3"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:text="@string/App"
      android:textAppearance="?android:attr/textAppearanceLarge" />

   <TextView
      android:id="@+id/textView7"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignBottom="@+id/textView5"
      android:layout_alignLeft="@+id/textView6"
      android:text="@string/Role"
      android:textAppearance="?android:attr/textAppearanceMedium"
      android:textSize="10sp" />

   <TextView
      android:id="@+id/textView5"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@+id/textView6"
      android:layout_marginTop="27dp"
      android:layout_toLeftOf="@+id/editText1"
      android:text="@string/LoginRole" />
   <TextView
      android:id="@+id/textView8"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_above="@+id/textView6"
      android:layout_alignLeft="@+id/textView5"
      android:layout_marginBottom="27dp"
      android:text="@string/method" />

   <TextView
      android:id="@+id/textView4"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/textView8"
      android:layout_below="@+id/button1"
      android:layout_marginTop="86dp"
      android:text="@string/LoginStatus" />


   <TextView
      android:id="@+id/textView6"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignTop="@+id/textView4"
      android:layout_centerHorizontal="true"
      android:text="@string/Status"
      android:textAppearance="?android:attr/textAppearanceMedium"
      android:textSize="10sp" />

   <TextView
      android:id="@+id/textView9"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignBottom="@+id/textView8"
      android:layout_alignLeft="@+id/textView6"
      android:text="@string/Choose"
      android:textAppearance="?android:attr/textAppearanceMedium"
      android:textSize="10sp" />

   <Button
      android:id="@+id/button2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerVertical="true"
      android:layout_toRightOf="@+id/textView6"
      android:onClick="loginPost"
      android:text="@string/LoginPost" />

   <Button
      android:id="@+id/button1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignBaseline="@+id/button2"
      android:layout_alignBottom="@+id/button2"
      android:layout_alignLeft="@+id/textView2"
      android:onClick="login"
      android:text="@string/LoginGet" />

   <TextView
      android:id="@+id/textView2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignBaseline="@+id/editText2"
      android:layout_alignBottom="@+id/editText2"
      android:layout_alignParentLeft="true"
      android:text="@string/Password" />

</RelativeLayout>

【问题讨论】:

  • 不要将 AsyncTask 用作单独的类。在 MainActivity 中使用这个类
  • 我也试过了,但是显示android.os.networkexception错误
  • 因为你没有在AndroidManifest.xml文件中添加INTERNET权限。
  • 发布您的 AndroidManifest.xml 代码。

标签: php android http android-asynctask


【解决方案1】:

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

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);
        }
 }

   }

别忘了在 Manifest.xml 文件中添加权限

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

【讨论】:

  • 在 btn.setOnClickListener(this) 中显示错误; View类型中的setOnClickListener(View.OnClickListener)方法不适用于参数(MessageBox)
  • 向这个监听器添加 cast 参数。像 btn.setOnClickListener((OnClickListener) this);
  • 通过使用 btn.setOnClickListener((OnClickListener) this);我的活动不幸停止了
  • 我已经编辑了答案,现在试试这个。你听说过调试器吗?
  • 使用调试器并检查是否存在问题。是哪一点导致您的应用程序停止。
猜你喜欢
  • 2022-11-20
  • 2016-01-13
  • 1970-01-01
  • 2018-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多