通过Android应用程序发送字符串到PHP文件 [英] Sending String to PHP file via android application
本文介绍了通过Android应用程序发送字符串到PHP文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我开发一个应用程序,其中包括所有接触到我的服务器我在做什么是显示所有的联系人,并派他们到我的服务器作为string.Here的上传我的code:
包com.example.core.freemusic;
进口java.io.IOException异常;
进口android.app.Activity;
进口android.app.ProgressDialog;
进口android.database.Cursor;
进口android.os.AsyncTask;
进口android.os.Bundle;
进口android.provider.ContactsContract;
进口android.widget.Button;
进口android.widget.LinearLayout;
进口android.widget.ListView;
进口android.widget.TextView;
进口org.apache.http.client.HttpClient;
进口org.apache.http.client.methods.HttpPost;
进口org.apache.http.impl.client.DefaultHttpClient;
公共类MainActivity延伸活动{
LL的LinearLayout;
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
LL =(的LinearLayout)findViewById(R.id.LinearLayout1);
LoadContactsAyscn LCA =新LoadContactsAyscn();
lca.execute();
}
类LoadContactsAyscn扩展的AsyncTask<虚空,虚空,字符串> {
ProgressDialog PD;
@覆盖
在preExecute保护无效(){
super.on preExecute();
PD = ProgressDialog.show(MainActivity.this,请等待,);
}
@覆盖
保护字符串doInBackground(虚空...... PARAMS){
串通讯录=;
光标C = getContentResolver()查询(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,NULL,NULL,NULL,NULL);
而(c.moveToNext()){
字符串CONTACTNAME = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
串phNumber = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
触点+ = +联系人姓名:+ phNumber +\\ n;
}
c.close();
返回接触;
}
@覆盖
保护无效onPostExecute(字符串触点){
super.onPostExecute(联系人);
pd.cancel();
TextView中A =(的TextView)findViewById(R.id.textView);
a.setText(联系人);
doInBackground(联系人);
}
保护无效doInBackground(字符串... PARAMS){
HttpClient的HttpClient的=新DefaultHttpClient();
HttpPost httppost =新HttpPost(http://192.168.2.30/b.php?username=+参数);
尝试{
httpclient.execute(httppost);
}赶上(IOException异常五){
e.printStackTrace();
}
}
}
}
其实,code显示没有错误,但在模拟器中运行它,它崩溃。
在logcat的错误是:
14 05-24:00:57.989 2488年至2500年/ com.example.core.freemusic W /艺术:暂停所有线程了:5.841ms
05-24 14:00:57.992 2488年至2500年/ com.example.core.freemusic I /艺术:背景部分并发标记扫描GC释放1888(115KB)AllocSpace对象,0(0B)LOS对象,50%的自由,502KB / 1014KB,暂停6.923ms总95.188ms
05-24 14:00:58.073 2488年至2488年/ com.example.core.freemusic I /编舞:跳过37帧!该应用程序可能会做它的主线程的工作太多了。
05-24 14:00:58.080 2488年至2488年/ com.example.core.freemusic D / gralloc_goldfish:模拟器不检测GPU的仿真。
05-24 14:00:58.102 2488年至2488年/ com.example.core.freemusic D / AndroidRuntime:关闭VM
05-24 14:00:58.103 2488年至2488年/ com.example.core.freemusic E / AndroidRuntime:致命异常:主要
工艺:com.example.core.freemusic,PID:2488
java.lang.IllegalArgumentException异常:在指数43查询非法字符:http://192.168.2.30/b.php?username=Sd:(546)456-4826
伤心:486-44
在java.net.URI.create(URI.java:730)
在org.apache.http.client.methods.HttpPost<&初始化GT;(HttpPost.java:79)
在com.example.core.freemusic.MainActivity $ LoadContactsAyscn.onPostExecute(MainActivity.java:64)
在com.example.core.freemusic.MainActivity $ LoadContactsAyscn.onPostExecute(MainActivity.java:38)
在android.os.AsyncTask.finish(AsyncTask.java:632)
在android.os.AsyncTask.access $ 600(AsyncTask.java:177)
在android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:645)
在android.os.Handler.dispatchMessage(Handler.java:102)
在android.os.Looper.loop(Looper.java:135)
在android.app.ActivityThread.main(ActivityThread.java:5221)
在java.lang.reflect.Method.invoke(本机方法)
在java.lang.reflect.Method.invoke(Method.java:372)
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:899)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
05-24 14:01:01.259 2488年至2488年/ com.example.core.freemusic I /工艺:发送信号。 PID:2488 SIG:9
05-24 15:24:12.790 2685年至2685年/ com.example.core.freemusic D / gralloc_goldfish:模拟器不检测GPU的仿真。
解决方案
在你的java:
HttpClient的客户端=新DefaultHttpClient();
HttpPost的PostMethod =新HttpPost(HTTP://localhost.index.php);
ArrayList的<&的NameValuePair GT; namevaluepairs中=新的ArrayList<&的NameValuePair GT;();
nameValuePairs.add(新BasicNameValuePair(用户名,1234567));
nameValuePairs.add(新BasicNameValuePair(密码,54321345));
postMethod.setEntity(新UrlEn codedFormEntity(namevaluepairs中,HTTP.UTF_8));
client.execute(getMethod);
在你的PHP:
$用户名= $ _ POST [用户名];
$密码= $ _ POST ['密码'];
I am developing an app which includes the upload of all contacts to my server what i am doing is displaying all the contacts and sending them to my server as a string.Here is my code:
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();
}
}
}
}
Actually the code shows no error but on running it in emulator it crashes. the error on logcat is:
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.
解决方案
In your 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);
In your PHP:
$username = $_POST['username'];
$password = $_POST['password'];
这篇关于通过Android应用程序发送字符串到PHP文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文