从Android到PHP页面发送数据

时间:2011-12-01 16:20:24

标签: php android http exception

我正在尝试将数据从Android应用程序(带有模拟器)发送到Web服务器(一个php页面)。首先,我用模拟器尝试过这个程序,它正在运行。之后,我用电话尝试了这个程序并发生了异常:

- > IO异常:操作超时。

我的代码的一部分:

HttpClient httpclient = new DefaultHttpClient();

HttpPost httppost = new HttpPost("http://10.0.2.2:90/takeDatas.php");
try {                   
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    nameValuePairs.add(new BasicNameValuePair("enlem", latitude ));
    nameValuePairs.add(new BasicNameValuePair("boylam", longitude ));
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    httpclient.execute(httppost);

} catch (ClientProtocolException e) {
    Toast.makeText(ReportLocationActivity.this, "Client protokol exception ", Toast.LENGTH_LONG).show();
} catch (IOException e) {
    Toast.makeText(ReportLocationActivity.this, "IO exception "+e.getMessage(), Toast.LENGTH_LONG).show();
}

我希望你能帮助我。 提前谢谢。

3 个答案:

答案 0 :(得分:2)

模拟器中的localhost是模拟器本身。不确定Windows环境,但在Linux下,我能够通过IP 10.0.2.2从仿真器访问主机系统上的Web服务器(注意:此IP不是我的Linux系统的IP,而是来自模拟器它可以通过这个IP完全访问。 您可以阅读有关模拟器网络here的更多信息。

答案 1 :(得分:1)

是,而不是使用127.0.0.1或http://localhost:90,您必须使用http://10.0.2.2/simpleSending.php

HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://10.0.2.2/yourpage.php");

    //This is the data to send
    String MyName = "flower"; //any data to send

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        nameValuePairs.add(new BasicNameValuePair("action", MyName));

        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request

        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String response = httpclient.execute(httppost, responseHandler);


        //This is the response from a php application
        String reverseString = response;
        Toast.makeText(this, "response" + reverseString, Toast.LENGTH_LONG).show();

    } catch (ClientProtocolException e) {
        Toast.makeText(this, "CPE response " + e.toString(), Toast.LENGTH_LONG).show();
        // TODO Auto-generated catch block
    } catch (IOException e) {
        Toast.makeText(this, "IOE response " + e.toString(), Toast.LENGTH_LONG).show();
        // TODO Auto-generated catch block
    }

答案 2 :(得分:1)

由于异步方法调用,可能会生成此错误。因此,您需要在类中包装该异步方法。 这是代码:

 package com.example.asynchttppost;

    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

    import android.app.Activity;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ProgressBar;
    import android.widget.Toast;

    public class MainActivity extends Activity implements OnClickListener{

        private EditText value;
        private Button btn;
        private ProgressBar pb;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            value=(EditText)findViewById(R.id.editText1);
            btn=(Button)findViewById(R.id.button1);
            pb=(ProgressBar)findViewById(R.id.progressBar1);
            pb.setVisibility(View.GONE);
            btn.setOnClickListener(this);
        }


        public void onClick(View v) {
            // TODO Auto-generated method stub
                if(value.getText().toString().length()<1){

                    // out of range
                    Toast.makeText(this, "please enter something", Toast.LENGTH_LONG).show();
                }else{
                    pb.setVisibility(View.VISIBLE);
                    new MyAsyncTask().execute(value.getText().toString());      
                }


        } 

        private class MyAsyncTask extends AsyncTask<String, Integer, Double>{

            @Override
            protected Double doInBackground(String... params) {
                // TODO Auto-generated method stub
                postData(params[0]);
                return null;
            }

            protected void onPostExecute(Double result){
                pb.setVisibility(View.GONE);
                Toast.makeText(getApplicationContext(), "Code Sent", Toast.LENGTH_SHORT).show();
            }
            protected void onProgressUpdate(Integer... progress){
                pb.setProgress(progress[0]);
            }

            public void postData(String valueIWantToSend) {
                // Create a new HttpClient and Post Header
                String url= "your website url";
                //String url ="http://10.0.2.2/remoteaccess/";
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(url);

                try {
                    // Add your data
                    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                    nameValuePairs.add(new BasicNameValuePair("myHttpData", valueIWantToSend));
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                    // Execute HTTP Post Request
                    HttpResponse response = httpclient.execute(httppost);

                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                }
            }

        }
    }
相关问题