我在PHP中有一个Web服务,它返回一个String,他重现了两个参数,id和te。 我用mozzila插件海报测试了它的使用,所以我决定将它用于我的Android应用程序。
这是我的android代码:
final String query = null;
AsyncHttpClient client = new AsyncHttpClient();
RequestParams rp = new RequestParams();
rp.put("id", num);
rp.put("te", tab);
Log.i("http","before send\n");
client.post("http://appdomain.hol.es/webService.php",rp, new JsonHttpResponseHandler(){
public void onSuccess(String jObject)
{
query.replace(query, jObject);
Log.i("http","recived: "+jObject+"\n");
}
public void onFailure(Throwable arg0)
{
Log.i("http","fail");
}
});
我正在调试logith.i,我可以看到它既没有显示也没有显示既没有失败。 有人可以帮我吗?
PD:我留下最相关的webService
$id = $_POST["id"];
$te = $_POST["te"];
$query = "SELECT `preg` , `respA` , `respB` , `respC` , `respD` , `respV`FROM `".$te."` WHERE `id` =".$id;
$resultado= mysql_query($query,$link);
$arraySalida = array();
while($registro = mysql_fetch_assoc ($resultado) ):
$cadena = "{$registro['preg']};{$registro['respA']};{$registro['respB']};{$registro['respC']};{$registro['respD']};{$registro['respV']}";
$arraySalida[]= $cadena;
endwhile;
echo implode(":",$arraySalida);
@jaimin的解决方案有效,但编译器说:类型不匹配:无法从AsyncTask转换为String中的字符串(!)
这是代码:
public String BBDD(int num, String tab)
{
HttpAsyncTask httpAsyncTask = new HttpAsyncTask(String.valueOf(num),tab);
/*(!)*/String resul = httpAsyncTask.execute("http://opofire.hol.es/webServiceOpoFire.php");
return resul;
}
答案 0 :(得分:0)
查看下面的代码,将数据发布到php
class PlaceOrder extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPst = new HttpPost(
"http://appdomain.hol.es/webService.php");
ArrayList<NameValuePair> parameters = new ArrayList<NameValuePair>(
2);
// add ur parameter here
parameters.add(new BasicNameValuePair("id", value1);
parameters.add(new BasicNameValuePair("te", value2);
httpPst.setEntity(new UrlEncodedFormEntity(parameters));
HttpResponse httpRes = httpClient.execute(httpPst);
String str=convertStreamToString(httpRes.getEntity().getContent()).toString();
Log.i("mlog","outfromurl"+str);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
public static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
答案 1 :(得分:0)
对于Http Post我建议您使用AsyncTask&lt;&gt;它将在UI的单独线程中运行 这是我两个月以来使用的代码,工作正常
private class HttpAsyncTask extends AsyncTask<String, Void, String> {
private String id,te;
public HttpAsyncTask(String id,String te){
this.id = id;
this.te = te;
}
@Override
protected String doInBackground(String... urls) {
return POST(urls[0]);
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
Toast.makeText(getBaseContext(), "Data Sent!", Toast.LENGTH_LONG).show();
}
}
public static String POST(String url){
InputStream inputStream = null;
String result = "";
try {
// 1. create HttpClient
HttpClient httpclient = new DefaultHttpClient();
// 2. make POST request to the given URL
HttpPost httpPost = new HttpPost(url);
// pass parameters in this way
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "value "));
nameValuePairs.add(new BasicNameValuePair("te", "value"));
//add data
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// 8. Execute POST request to the given URL
HttpResponse httpResponse = httpclient.execute(httpPost);
// 9. receive response as inputStream
inputStream = httpResponse.getEntity().getContent();
// 10. convert inputstream to string
if(inputStream != null)
result = convertInputStreamToString(inputStream);
else
result = "Did not work!";
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
// 11. return result
return result;
}
private static String convertInputStreamToString(InputStream inputStream) throws IOException {
// TODO Auto-generated method stub
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
String line = "";
String result = "";
while((line = bufferedReader.readLine()) != null)
result += line;
inputStream.close();
return result;
}
传递值可以创建像这样的构造函数。
形成您的活动吗
HttpAsyncTask httpAsyncTask = new HttpAsyncTask(id,te);//this will pass variables values
String ResultfromServer = httpAsyncTask.execute(urlStr);// String ResultfromServer is your response string
在AsyncTask中我创建了构造函数
这对我很有帮助,希望它也能帮到你