Android和PHP创建API
我正试图为我的Android应用程序设置一个PHP API进行交互,我的问题是post数据似乎从未发布过,我也无法从给定的URL检索响应体(HTML/TXT) 我的代码Android和PHP创建API,php,android,html,http,post,Php,Android,Html,Http,Post,我正试图为我的Android应用程序设置一个PHP API进行交互,我的问题是post数据似乎从未发布过,我也无法从给定的URL检索响应体(HTML/TXT) 我的代码 Thread thread = new Thread(new Runnable(){ @Override public void run() { try { HttpClient client = new DefaultHttpClient(
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
try {
HttpClient client = new DefaultHttpClient();
HttpPost clientpost = new HttpPost("http://192.168.1.129/updateMain.php");
try {
List<NameValuePair> params = new ArrayList<NameValuePair>(2);
params.add(new BasicNameValuePair("_id", "1"));
params.add(new BasicNameValuePair("job_name", "Test"));
clientpost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse response = client.execute(clientpost);
String responseBody = EntityUtils.toString(response.getEntity());
} catch(Exception e)
{
Log.e("Error", e.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
Thread-Thread=新线程(new Runnable()){
@凌驾
公开募捐{
试一试{
HttpClient=new DefaultHttpClient();
HttpPost clientpost=新的HttpPost(“http://192.168.1.129/updateMain.php");
试一试{
列表参数=新的ArrayList(2);
参数add(新的BasicNameValuePair(“_id”,“1”));
参数添加(新的BasicNameValuePair(“作业名称”、“测试”);
setEntity(新的UrlEncodedFormEntity(参数));
HttpResponse response=client.execute(clientpost);
String ResponseBy=EntityUtils.toString(response.getEntity());
}捕获(例外e)
{
Log.e(“Error”,e.toString());
}
}捕获(例外e){
e、 printStackTrace();
}
}
});
thread.start();
如果我可以发布数据,那么我就能够将JSON发布到服务器并从服务器检索JSON,从而克服了我目前面临的最大障碍
非常感谢您的帮助 publicstringgetresponse(字符串url,列表nameValuePairs){
public String getResponse(String url, List<NameValuePair> nameValuePairs) {
url = url.replaceAll(" ", "%20");
String result = "ERROR";
Log.d("Pair", nameValuePairs.toString());
HttpClient httpclient = new DefaultHttpClient();
HttpParams http_params = httpclient.getParams();
http_params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION,
HttpVersion.HTTP_1_1);
HttpConnectionParams.setConnectionTimeout(http_params, TIMEOUT);
HttpConnectionParams.setSoTimeout(http_params, TIMEOUT);
HttpResponse response = null;
try {
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
e.printStackTrace();
result = "ERROR";
} catch (IOException e) {
e.printStackTrace();
result = "ERROR";
}
try {
// Get hold of the response entity
HttpEntity entity = null;
if (response != null) {
entity = response.getEntity();
}
if (entity != null) {
InputStream inputStream = entity.getContent();
result = convertStreamToString(inputStream);
}
} catch (IOException e) {
result = "ERROR";
}
httpclient.getConnectionManager().shutdown();
return result;
}
url=url.replaceAll(“,“%20”);
字符串result=“ERROR”;
Log.d(“Pair”,nameValuePairs.toString());
HttpClient HttpClient=新的DefaultHttpClient();
HttpParams http_params=httpclient.getParams();
http_params.setParameter(CoreProtocolPNames.PROTOCOL_版本,
HTTP_1_1);
setConnectionTimeout(http_参数,超时);
HttpConnectionParams.setSoTimeout(http_参数,超时);
HttpResponse响应=null;
试一试{
HttpPost HttpPost=新的HttpPost(url);
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
response=httpclient.execute(httppost);
}捕获(客户端协议例外e){
e、 printStackTrace();
result=“ERROR”;
}捕获(IOE异常){
e、 printStackTrace();
result=“ERROR”;
}
试一试{
//获取响应实体
HttpEntity=null;
if(响应!=null){
entity=response.getEntity();
}
如果(实体!=null){
InputStream InputStream=entity.getContent();
结果=convertStreamToString(inputStream);
}
}捕获(IOE异常){
result=“ERROR”;
}
httpclient.getConnectionManager().shutdown();
返回结果;
}
将此方法用于AsyncTask或后台线程此方法适合我。请务必注意异常和错误处理。Key121变量是您的php文件url
class callServiceTask extends AsyncTask<Void, Void, Void>
{
@Override
protected Void doInBackground(Void... params) {
loginCheck();
return null;
}
}
public void loginCheck()
{
InputStream is = null;
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("aa","11"));
nameValuePairs.add(new BasicNameValuePair("bb","22"));
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(KEY_121);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e)
{
Log.e("log_tag", "Error:"+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
Log.e("log_tag", "----------------"+result);
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
}
class callServiceTask扩展了AsyncTask
{
@凌驾
受保护的Void doInBackground(Void…参数){
登录检查();
返回null;
}
}
公共无效登录检查()
{
InputStream=null;
ArrayList nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“aa”,“11”));
添加(新的BasicNameValuePair(“bb”,“22”));
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(键号121);
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
is=entity.getContent();
}捕获(例外e)
{
Log.e(“Log_标记”,“错误:+e.toString());
}
//将响应转换为字符串
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is,“iso-8859-1”),8;
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
is.close();
结果=sb.toString();
Log.e(“Log_tag”、“---------------------------------”+结果);
}捕获(例外e){
Log.e(“Log_标记”,“错误转换结果”+e.toString());
}
}
您需要使用流和缓冲读取器来分析响应。一定要检查并让我们知道。考虑使用截击库,它更快、更容易。我检查了截击,同样,回答永远不会打印出来。。。但就其本身而言,这将是一个不同的问题。谢谢你的建议。不幸的是,这对我不起作用,响应永远不会被记录到日志中。d(“响应”,result)永远不会打印任何东西。另外,我添加了捕获,并在另一个线程中运行它。@JamesParker:在应答中更新了完整的类代码。检查并让我知道您的输出/错误。在这里,我也需要您的php文件,正如您所说,即使其他方法也不提供输出。只打印错误,从不打印堆栈跟踪。对于这样一个简单的任务,这种方法似乎都很复杂?我可能错了,但这很难理解。