Android对PHP的POST请求 [英] Android POST Request to PHP

查看:52
本文介绍了Android对PHP的POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试开发一个Android应用程序.在此应用程序中,我需要将POST请求发送到PHP页面.

I try to develope an Android application. In this application, I need to send a POST request to a PHP page.

我的代码在Java端:

My code is at Java side:

 DefaultHttpClient httpclient = new DefaultHttpClient();
 HttpPost httppost = new HttpPost("http://localhost/get.php");

     ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
     nameValuePairs.add(new BasicNameValuePair("mail", "asdasd"));
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

     try{
            HttpResponse httpresponse = httpclient.execute(httppost);

     }catch(Exception e){
         Toast.makeText(getApplicationContext(), "Didn't Happen",10).show();    
     }

Manifest.xml

Manifest.xml

 <?
xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.misman"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="17"/>
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:permission="android.permission.INTERNET"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.misman.MainActivity"
            android:label="@string/app_name" >

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

我从很多网站上搜索过,并且代码不断进入catch块中.我还使用了HttpURLConnection,但没有用.您能帮我什么问题吗?

I have searched from a lot of websites and the code goes in catch block continuously.I also used HttpURLConnection and didn't work. Can you help me what can be the problem?

推荐答案

我的问题是我想在应用程序主线程中运行代码,而主线程不允许这样做.有几种方法可以解决此问题,

My problem was that I want to run my code in application main thread and main thread doesn't allow that. There are a few ways to solve this problem,

  1. 使用 AsyncTask 执行后台操作
  2. 在新线程中运行代码
  3. MainActivity 中启用网络操作,这是我现在选择的方式

  1. Using AsyncTask to perform background operation
  2. Run the code in a new thread
  3. Enable network operation in MainActivity and this is what I choose for now by

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().
                                                detectNetwork().build();
StrictMode.setThreadPolicy(policy);

更新

由于问题的名称,我想分享如何在android中进行Http调用以指导人们.有一些API:

Because of the name of the question I want to share how to make Http call in android in order to guide people. There are some apis:

这里有培训

这篇关于Android对PHP的POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆