Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在android中正确制作httppost_Php_Android_Http Post - Fatal编程技术网

Php 在android中正确制作httppost

Php 在android中正确制作httppost,php,android,http-post,Php,Android,Http Post,我正在尝试制作一个与php文件/数据库通信的android应用程序。 我无法让它工作,我对这种android编程完全是新手。 我已经从教程中了解到了这一点: new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int id) { name = userInput.getText().toString(); HttpClient httpclient = new DefaultH

我正在尝试制作一个与php文件/数据库通信的android应用程序。
我无法让它工作,我对这种android编程完全是新手。
我已经从教程中了解到了这一点:

new DialogInterface.OnClickListener() {
 public void onClick(DialogInterface dialog,int id) {
 name = userInput.getText().toString();
 HttpClient httpclient = new DefaultHttpClient();
 HttpPost httppost = new HttpPost("http://www.test.com/register.php");
 try {
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
      nameValuePairs.add(new BasicNameValuePair("name", name));
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
      HttpResponse response = httpclient.execute(httppost);

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


}
当我启动应用程序时,它会立即崩溃。
我做错了什么?
此代码仍然有效吗?
我忘了什么吗?

当我启动应用程序时,它会立即崩溃

您应该在后台线程中调用http请求。您可以为此使用AsyncTask

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

    @Override
    protected String doInBackground(String... params) {
      HttpClient httpclient = new DefaultHttpClient();
 HttpPost httppost = new HttpPost("http://www.test.com/register.php");
 try {
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
      nameValuePairs.add(new BasicNameValuePair("name", params[0]));
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
      HttpResponse response = httpclient.execute(httppost);

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

        return "";
    }

    @Override
    protected void onPostExecute(String result) {

    }

    @Override
    protected void onPreExecute() {}

    @Override
    protected void onProgressUpdate(Void... values) {}
}
私有类LongOperation扩展了异步任务{
@凌驾
受保护的字符串doInBackground(字符串…参数){
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://www.test.com/register.php");
试一试{
List nameValuePairs=新的ArrayList(1);
添加(新的BasicNameValuePair(“名称”,参数[0]);
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
}捕获(IOE异常){
//TODO自动生成的捕捉块
}
返回“”;
}
@凌驾
受保护的void onPostExecute(字符串结果){
}
@凌驾
受保护的void onPreExecute(){}
@凌驾
受保护的void onProgressUpdate(void…值){}
}

您可以使用
new LongOperation()运行它

所有数据库通信都应在后台完成。这可以通过使用AsyncTask或其他Worker来完成。您还需要Internet权限,请将此行添加到清单:

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


但我建议您对API调用使用一个类似于翻新的库。这就省去了你的麻烦,加上GSON,它的功能非常强大。

首先,在安卓系统中使用谷歌
AsynchTask
,了解它是什么。请记住,所有与网络相关的操作都必须在单独的线程中完成,而不是在主android UI线程中完成。因此,您正在尝试的代码应该如下所示

new DialogInterface.OnClickListener() {
 public void onClick(DialogInterface dialog,int id) {
 name = userInput.getText().toString();
 new CallPostService().execute();
}

class CallPostService extends AsyncTask<String, String, String> {
ProgressDialog progressDialog;

        @Override
        protected void onPreExecute() {
            progressDialog = new ProgressDialog(YourActivity.this);
            progressDialog.setMessage("Please wait...");
            progressDialog.show();
        }
        @Override
        protected String doInBackground(String... params) {
           HttpClient httpclient = new DefaultHttpClient();
 HttpPost httppost = new HttpPost("http://www.test.com/register.php");
 try {
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
      nameValuePairs.add(new BasicNameValuePair("name", name));
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
      HttpResponse response = httpclient.execute(httppost);

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

        @Override
        protected void onPostExecute(String response) {

       progressDialog.dismiss();
    }
new DialogInterface.OnClickListener(){
public void onClick(DialogInterface对话框,int-id){
name=userInput.getText().toString();
新建CallPostService().execute();
}
类CallPostService扩展了AsyncTask{
进行对话进行对话;
@凌驾
受保护的void onPreExecute(){
progressDialog=新建progressDialog(YourActivity.this);
progressDialog.setMessage(“请稍候…”);
progressDialog.show();
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://www.test.com/register.php");
试一试{
List nameValuePairs=新的ArrayList(1);
添加(新的BasicNameValuePair(“name”,name));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
}捕获(IOE异常){
//TODO自动生成的捕捉块
}
返回null;
}
@凌驾
受保护的void onPostExecute(字符串响应){
progressDialog.disclose();
}
并记住在manifest.xml文件中添加以下行

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

检查此代码,它将帮助您

检查我的回答共享日志哪里可以找到日志?你有一些例子说明如何做到这一点吗?你需要导入android.os.AsyncTask;在哪里添加我的用户输入?谢谢,但它仍然说无法解析符号execute@Rewtie对不起,我忘了一些东西..使用这一行。new LongOperation().execute(userinputString1);
<uses-permission android:name="android.permission.INTERNET"/>