Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android http post+;web服务PHP_Android_Web Services_Http Post - Fatal编程技术网

Android http post+;web服务PHP

Android http post+;web服务PHP,android,web-services,http-post,Android,Web Services,Http Post,我有一个PHP的web服务,它返回一个字符串,他接收两个参数,id和te。 我已经用mozzila插件海报测试了它的使用,所以我决定在我的android应用程序中使用它 这是我的android代码: final String query = null; AsyncHttpClient client = new AsyncHttpClient(); RequestParams rp = new RequestParams(); rp.put("id", num); rp.put("te", ta

我有一个PHP的web服务,它返回一个字符串,他接收两个参数,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");   
    }
});
我正在调试日志。我和我都可以看到它既不显示“未接收”也不显示“未失败”。 有人能帮我吗

PD:我离开了最相关的Web服务

$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转换为字符串

代码如下:

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;
}

查看下面用于将数据发布到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();

    }
class PlaceOrder扩展了异步任务{
@凌驾
受保护的Void doInBackground(Void…参数){
//TODO自动生成的方法存根
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost httpPst=新的HttpPost(
"http://appdomain.hol.es/webService.php");
ArrayList参数=新建ArrayList(
2);
//在此处添加ur参数
添加(新的BasicNameValuePair(“id”,value1);
添加(新的BasicNameValuePair(“te”,值2);
setEntity(新的UrlEncodedFormEntity(参数));
HttpResponse httpRes=httpClient.execute(httpPst);
String str=convertStreamToString(httpRes.getEntity().getContent()).toString();
Log.i(“mlog”、“outfromurl”+str);
}捕获(不支持的编码异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回null;
}
}
公共静态字符串convertStreamToString(InputStream为){
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is));
StringBuilder sb=新的StringBuilder();
字符串行=null;
试一试{
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
}捕获(例外e){
e、 printStackTrace();
}最后{
试一试{
is.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
使某人返回字符串();
}

对于Http Post,我建议您使用AsyncTask,它将在与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;

}
在AsyncTask中,我创建了构造函数


这对我很有帮助,希望它也能帮助你

什么是响应类型json或计划字符串?@adcom string,如果你愿意,我可以编辑帖子并编写de-webService代码你只想使用AsyncHttpClient吗?有没有其他代码适合你?@adcom没有,真的有什么东西能让服务器发送给我的字符串值得我使用的吗de from@jaimin,thanksoutfromurl将告诉你服务器的输出你得到了什么…你可以在你的活动中编写这个类,并将其称为new PlaceOrder().execute();谢谢,但我不太明白…->itemDetails…声明在哪里?->Void doInBackground(Void…params)void..params是正确的吗?好的,我会向你解释,你用过asyntask吗?知道这个概念吗?我从来没有用过asyntask,我知道它是用来在后台做一些事情的基本上是为了向服务器发布数据我建议你使用asynctask代码…看我更新的ansere基本上我需要传递的密钥和值我写了以下内容:HttpAsyncTask HttpAsyncTask=new HttpAsyncTask(String.valueOf(numero),tab);String Resu=HttpAsyncTask.execute(“);但编译器说:类型不匹配:无法从AsyncTask转换为字符串。有什么问题吗?在问题中发布日志猫无法访问此方法:公共静态字符串发布(字符串url)?无法访问此方法:公共静态字符串POST(字符串url)?它从服务器返回结果我无法发布muy logcat,因为他不允许我编译它让我再看一次
 HttpAsyncTask httpAsyncTask = new HttpAsyncTask(id,te);//this will pass variables values 
 String  ResultfromServer = httpAsyncTask.execute(urlStr);// String ResultfromServer is your response string