我的代码在android中不起作用(http Get)

时间:2013-01-12 17:13:56

标签: android http

我想从网站加载网页并在edittext上显示其来源,如登录,我将登录信息发送到网站,如果网站返回“确定”,我转到下一个表单,但它不起作用,它表现出了反应:

结帐我的代码:

     package com.arash.rz.webtar;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;


public  class MainActivity extends Activity implements AsyncTaskCompleteListener<String> {

    public String SendReuqest(String command,String variables)
    {
        String response = null;
        String url="api.php?cmd="+command+"&varz="+variables;
        return response;
    }
    public void MessageBox(String message)
    {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
        }  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        final Button btnlogin = (Button)findViewById(R.id.button1);
        final Button btnregister = (Button)findViewById(R.id.button2);
        final EditText txtemail = (EditText)findViewById(R.id.editText1);
        final EditText txtepass = (EditText)findViewById(R.id.editText2);

        btnlogin.setFilterTouchesWhenObscured(true);

        btnlogin.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                readWebpage(v);
                }
              // Perform action on click
          }
          );


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.menu_settings:
            finish();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }







    }
    @Override
    public void onTaskComplete(String result) {
        // TODO Auto-generated method stub

    }










     private class DownloadWebPageTask extends AsyncTask<String, Void, String> {

         @Override

         protected String doInBackground(String... urls) {

                String response = "";

                for (String url : urls) {

                      DefaultHttpClient client = new DefaultHttpClient();

                      HttpGet httpGet = new HttpGet(url);

                      try {

                             HttpResponse execute = client.execute(httpGet);

                             InputStream content =
                                          execute.getEntity().getContent();



                             BufferedReader buffer = new BufferedReader(

                                           new InputStreamReader(content));

                             String s = "";

                             while ((s = buffer.readLine()) != null) {

                                    response += s;

                             }

                      } catch (Exception e) {

                             e.printStackTrace();

                      }

                }

                return response;

         }



         protected void onPostExecute(String result) {

                //textView.setText(result);
                MessageBox("Returned:"+result);
                EditText ed = (EditText)findViewById(R.id.editText1);
                ed.setText(result);

         }

  }



  public void readWebpage(View view) {

         DownloadWebPageTask task = new DownloadWebPageTask();

         task.execute(new String[] { "http://127.0.0.1" });



  }

2 个答案:

答案 0 :(得分:0)

使用Fiddler工具跟踪网络活动。它将显示从服务器获取的请求和响应的跟踪。通过查看提琴手,您可以确认问题出在客户端或服务器上。 谢谢,

答案 1 :(得分:0)

您是否已将以下行添加到AndroidManifest.xml

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