【问题标题】:Sending Data From Android To PHP Page将数据从 Android 发送到 PHP 页面
【发布时间】:2011-12-01 16:20:24
【问题描述】:

我现在正在尝试将数据从 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();
}

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

【问题讨论】:

  • 您可以使用相同的 URL 从浏览器连接吗?
  • 您是否在您的 android manifest 中添加了互联网连接权限?
  • 我认为 android 模拟器是一个独立的虚拟机,它无法访问本地主机域。正如 Guillaurne 建议的那样,尝试从您的模拟器 Web 浏览器访问此 url 以缩小您的问题(如果正确的话是与代码无关..)
  • @Joe & Guillaume - 是的,我没有从我的模拟器浏览器连接localhost:90。这个问题我该怎么办?

标签: php android http exception


【解决方案1】:

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

【讨论】:

  • 是的,我使用的是 10.0.2.2:90/simpleSending.php,没有连接错误。谢谢.. PHP 错误仍然存​​在:“注意:未定义的索引:第 3 行 C:\wamp\www\simpleSending.php 中的操作”
【解决方案2】:

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

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
    }

【讨论】:

  • 如果您将使用10.0.2.2:90/yourpage.php,则无响应。改为使用,10.0.2.2/yourpage.php
  • 如果我使用 10.0.2.2/mypage.php ,错误消息将显示在模拟器屏幕上:CPE 响应 org.apache.http.client.HttpResponseException:Not Found
  • 首先将你的php代码设为$_REQUEST,它将支持get和post。并转到浏览器并像这样检查它localhost/yourpage.php?action=hussain。有了这个,10.0.2.2/yourpage.php,它在这里工作正常
  • 请将您的php文件放到wampp的www文件夹中。当文件不存在时,会出现此“CPE 响应 org.apache.http.client.HttpResponseException”。我想,它已经解决了
  • @mH16- 很抱歉我的延迟回答。首先,我的 php 文件也在 wampp 的 www 文件夹中。另外,当我使用 localhost/mypage.php?action=hussain 时,出现 http 错误(404 Not Found),但是当我使用 localhost:90/mypage.php?action=hussain 时在浏览器上没有错误(显示 niassuh )。我无法理解这个问题的解决方案。
【解决方案3】:

此错误可能是由于异步方法调用而产生的。因此您需要将该异步方法包装在一个类中。 代码如下:

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

        }
    }

【讨论】:

    猜你喜欢
    • 2012-01-19
    • 1970-01-01
    • 1970-01-01
    • 2011-09-22
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 2020-08-16
    相关资源
    最近更新 更多