试图从Android应用程序传递参数到PHP服务器

时间:2014-02-07 12:49:47

标签: php android http get

我的问题是这样的:我想在Android应用程序中调用url并通过这种方式在uri中提供我的webserver参数

myserver.com/index.php?name=peter&age=22

参数是:

名=彼得

年龄= 22

服务器已在运行,但我无法使用我的Android应用程序代码访问它... 是的,我有清单中的许可......

检查我是否在线......

感谢您的帮助。

                try{
                    HttpClient client = new DefaultHttpClient();
                    String getURL = "irgendeinurl.web44.net/index.php?vorname=maria asdf&email=me@mail.com&phone=0786655962&time=1500&date=160914&merk=wollte nachfragen wegem Termin :) tschüss";
                    HttpGet get = new HttpGet(getURL);
                    HttpResponse responseGet = client.execute(get);
                    HttpEntity resEntityGet = responseGet.getEntity();
                if (resEntityGet != null) {
                    //connection success
                    Log.i("GET RESPONSE",EntityUtils.toString(resEntityGet));
                }

                } catch (Exception e) {
                    e.printStackTrace();
                }

这是我得到的:

02-07 14:04:44.062:我/网上?(27169):是的 02-07 14:04:44.072:W / System.err(27169):java.lang.IllegalStateException:目标主机不能为空,或者在参数中设置。 scheme = null,host = null,path = irgendeinurl.web44.net / index.php 02-07 14:04:44.072:W / System.err(27169):at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:591) 02-07 14:04:44.072:W / System.err(27169):at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:293) 02-07 14:04:44.072:W / System.err(27169):at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) 02-07 14:04:44.072:W / System.err(27169):at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 02-07 14:04:44.072:W / System.err(27169):at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) 02-07 14:04:44.082:W / System.err(27169):at ch.mzg.app.Termin3 $ 2.onClick(Termin3.java:106) 02-07 14:04:44.082:W / System.err(27169):在android.view.View.performClick(View.java:3511) 02-07 14:04:44.082:W / System.err(27169):在android.view.View $ PerformClick.run(View.java:14105) 02-07 14:04:44.082:W / System.err(27169):在android.os.Handler.handleCallback(Handler.java:605) 02-07 14:04:44.082:W / System.err(27169):在android.os.Handler.dispatchMessage(Handler.java:92) 02-07 14:04:44.082:W / System.err(27169):在android.os.Looper.loop(Looper.java:137) 02-07 14:04:44.092:W / System.err(27169):在android.app.ActivityThread.main(ActivityThread.java:4482) 02-07 14:04:44.092:W / System.err(27169):at java.lang.reflect.Method.invokeNative(Native Method) 02-07 14:04:44.092:W / System.err(27169):at java.lang.reflect.Method.invoke(Method.java:511) 02-07 14:04:44.092:W / System.err(27169):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:787) 02-07 14:04:44.092:W / System.err(27169):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554) 02-07 14:04:44.092:W / System.err(27169):at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:0)

您必须使用以下内容对参数进行编码:

String query = URLEncoder.encode("vorname=maria asdf&email=me@mail.com&phone=0786655962&time=1500&date=160914&merk=wollte nachfragen wegem Termin :) tschüss", "utf-8");
String getURL = "http://irgendeinurl.web44.net/index.php?" + query;

答案 1 :(得分:0)

以下代码适用于我...在调用此函数之前,参数是URL编码的......您可以根据需要自定义返回值...

如果您想要更多内容,请告诉我......

public class GenericNetworkAction {


        Context context;
        boolean asynch;
        String returnResponse = "Error";
        String parameters = "";
        public GenericNetworkAction(Context passedContext, boolean passedAsynch, String passedParameters) {
            context = passedContext;
            asynch = passedAsynch;
            parameters = passedParameters;

        }

        public void HandleNetworkActivity() {
               String stringUrl = Constants.baseURL;
               stringUrl = stringUrl + parameters;
                ConnectivityManager connMgr = (ConnectivityManager) 
                context.getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
                if (networkInfo != null && networkInfo.isConnected()) {
                    if (asynch)
                    new DownloadWebpageTask().execute(stringUrl);
                    else
                        returnResponse = getServerResponse(stringUrl);
                } else {
    //              Toast.makeText(context, 
    //                      context.getResources().getText(R.string.err_network_na), Toast.LENGTH_SHORT).show();
                    Log.e("Error", "Error connecting to the network");
                    return;
                    }
        }
        private class DownloadWebpageTask extends AsyncTask<String, Void, String> {

            @Override
            protected String doInBackground(String... strArrayUrl) {
                try {
                    return getServerResponse(strArrayUrl[0]);
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
                return "";
            }
            @Override
            protected void onPostExecute(String strResults) {

                returnResponse = strResults;
            }  

            private String getServerResponse(String strUrl) {
                InputStream localInputStream;
                try {
                    URL localUrl = new URL (strUrl);
                    HttpURLConnection localConn = (HttpURLConnection) localUrl.openConnection();
                    localConn.setReadTimeout(10000);
                    localConn.setConnectTimeout(15000);
                    localConn.setRequestMethod("POST");
                    localConn.setDoInput(true);
                    //localConn.setDoOutput(true);
                    localConn.connect();
                    localConn.getErrorStream();

                    int iResponseCode = localConn.getResponseCode();
                    localInputStream = localConn.getErrorStream();

                    byte[] buffer = new byte[1000000];
                    int iReturnNoOfBytes, iOffset=0;
                    iReturnNoOfBytes = localInputStream.read(buffer);
                     localConn.disconnect();
                    return new String(buffer);
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
                return "";
            }

        }
        private String getServerResponse(String strUrl) {
            InputStream localInputStream;
            try {
                //strUrl = URLEncoder.encode(strUrl, "UTF-8");
                URL localUrl = new URL (strUrl);
                HttpURLConnection localConn = (HttpURLConnection) localUrl.openConnection();
                localConn.setReadTimeout(10000);
                localConn.setConnectTimeout(15000);
                localConn.setRequestMethod("POST");
                localConn.setDoInput(true);
                localConn.connect();


                int iResponseCode = localConn.getResponseCode();
                localInputStream = localConn.getInputStream();

                byte[] buffer = new byte[1000000];
                int iReturnNoOfBytes, iOffset=0;
                iReturnNoOfBytes = localInputStream.read(buffer);
                 localConn.disconnect();
                 Log.i("query", strUrl);
                return new String(buffer);
            }
            catch (Exception e) {
                e.printStackTrace();
            }
            return "";
        }

    }

答案 2 :(得分:0)

在域名前添加http:// 修改下面的网址..

    String getURL = "http://irgendeinurl.web44.net/index.php?vorname=maria      asdf&email=me@mail.com&phone=0786655962&time=1500&date=160914&merk=wollte nachfragen wegem Termin :) tschüss";