通过android应用程序将字符串发送到PHP文件

时间:2015-05-24 10:17:45

标签: java android

我正在开发一个应用程序,其中包括将所有联系人上传到我的服务器,我正在做的是显示所有联系人并将其作为字符串发送到我的服务器。这是我的代码:

package com.example.core.freemusic;
import java.io.IOException;
import android.app.Activity;
import android.app.ProgressDialog;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
public class MainActivity extends Activity {
LinearLayout ll;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ll = (LinearLayout) findViewById(R.id.LinearLayout1);
    LoadContactsAyscn lca = new LoadContactsAyscn();
    lca.execute();
}
class LoadContactsAyscn extends AsyncTask < Void, Void, String > {
    ProgressDialog pd;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pd = ProgressDialog.show(MainActivity.this, "Please Wait","");
    }
    @Override
     protected String doInBackground(Void...params) {
        String contacts = "";
        Cursor c = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
        while (c.moveToNext()) {
            String contactName = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String phNumber = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            contacts += contactName + ":" + phNumber + "\n";
        }
        c.close();
        return contacts;
    }
    @Override
     protected void onPostExecute(String contacts) {
        super.onPostExecute(contacts);
        pd.cancel();
        TextView a = (TextView) findViewById(R.id.textView);
        a.setText(contacts);
        doInBackground(contacts);
    }
    protected void doInBackground(String... params) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://192.168.2.30/b.php?username="+params);
        try {
            httpclient.execute(httppost);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
}

实际上代码没有显示错误,但是在模拟器中运行它会崩溃。 logcat上的错误是:

05-24 14:00:57.989    2488-2500/com.example.core.freemusic W/art﹕    Suspending all threads took: 5.841ms
05-24 14:00:57.992    2488-2500/com.example.core.freemusic I/art﹕ Background partial concurrent mark sweep GC freed 1888(115KB) AllocSpace objects, 0(0B) LOS objects, 50% free, 502KB/1014KB, paused 6.923ms total 95.188ms
05-24 14:00:58.073    2488-2488/com.example.core.freemusic I/Choreographer﹕ Skipped 37 frames!  The application may be doing too much work on its main thread.
05-24 14:00:58.080    2488-2488/com.example.core.freemusic D/gralloc_goldfish﹕ Emulator without GPU emulation detected.
05-24 14:00:58.102    2488-2488/com.example.core.freemusic D/AndroidRuntime﹕ Shutting down VM
05-24 14:00:58.103    2488-2488/com.example.core.freemusic E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.core.freemusic, PID: 2488
java.lang.IllegalArgumentException: Illegal character in query at index 43: http://192.168.2.30/b.php?username=Sd:(546) 456-4826
Sad:486-44
        at java.net.URI.create(URI.java:730)
        at org.apache.http.client.methods.HttpPost.<init>(HttpPost.java:79)
        at com.example.core.freemusic.MainActivity$LoadContactsAyscn.onPostExecute(MainActivity.java:64)
        at com.example.core.freemusic.MainActivity$LoadContactsAyscn.onPostExecute(MainActivity.java:38)
        at android.os.AsyncTask.finish(AsyncTask.java:632)
        at android.os.AsyncTask.access$600(AsyncTask.java:177)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
05-24 14:01:01.259    2488-2488/com.example.core.freemusic I/Process﹕ Sending signal. PID: 2488 SIG: 9
05-24 15:24:12.790    2685-2685/com.example.core.freemusic D/gralloc_goldfish﹕ Emulator without GPU emulation detected.

2 个答案:

答案 0 :(得分:1)

在你的java中:

HttpClient client=new DefaultHttpClient();
HttpPost postMethod=new HttpPost("http://localhost.index.php");
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("username","1234567"));
nameValuePairs.add(new BasicNameValuePair("password","54321345"));
postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8));
client.execute(getMethod);

在你的PHP中:

$username = $_POST['username'];
$password = $_POST['password'];

答案 1 :(得分:0)

作为错误代码状态,您需要对查询进行编码:

HttpPost httppost = new HttpPost("http://192.168.2.30/b.php?username="+params);

需要改为:

HttpPost httppost = new HttpPost("http://192.168.2.30/b.php?username="+java.net.URLEncoder.encode(params[0], "UTF-8"));