请求将来继续发送空参数

时间:2017-05-21 21:43:10

标签: java php android json asynchronous

我试图通过RequestFuture将参数传递给php文件  的AsyncTask。我执行它的类是我的主要活动,但是  问题是,当我执行asynctask时,它还没有  派遣参赛者。最近我得到了一个  奇怪的错误这是我的主要活动

#include <Rcpp.h>
#include <RcppGSL.h>

#include <iostream>
#include <stdlib.h>

#include <cstdlib>
#include <iostream>
#include <stdlib.h>
#include <stdexcept>
#include <gsl/gsl_matrix.h>

#include <gsl/gsl_permutation.h>

#include "graphm/graph.h"
#include "graphm/experiment.h"
#include "graphm/algorithm.h"

using namespace std;
using namespace Rcpp;

// declare a dependency on the RcppGSL package; also activates plugin
// (but not needed when ’LinkingTo: RcppGSL’ is used with a package)
//
// [[Rcpp::depends(RcppGSL,Rcpp)]]
// [[Rcpp::export]]
Rcpp::List run_graph_match(const RcppGSL::Matrix& A, const RcppGSL::Matrix& B, const Rcpp::List& algorithm_params){
// .. body
}
  

你可以看到我正在执行这里的异步类

public class MainActivity extends AppCompatActivity {
    public TextView textv;
    private EditText username;
    private EditText password;
    public ProgressDialog progressDialog;
    public Context c;
    public parseActivity a;
    private JSONObject params = new JSONObject();
    public JSONObject js;
    public static String stat;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        a = new parseActivity();
        username = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password);
        textv = (TextView) findViewById(R.id.view);
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Logging in...");
        final Button logb = (Button) findViewById(R.id.login);

        logb.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                if(view == logb) {

                        String user = username.getText().toString();
                        String pass = password.getText().toString();

                    try {
                        params.put("username", user);
                        params.put("password", pass);
                        Log.w("tag", "doInBackground: "+params.toString());
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    textv.setText(params.toString());
                       c = getApplicationContext();
                       caller caller = new caller(params,c);
                       caller.execute();
                }

            }
        });

    }

}

这是我的PHP代码没有错误。

public class caller extends AsyncTask<Void, Void, JSONObject> {
    private parseActivity p = new parseActivity();
    private JSONObject params;
    private Context c;
    private JSONObject js;
    public caller(JSONObject  param, Context context){
       params = param;
       c = context;
    }

    @Override
    protected JSONObject doInBackground(Void...param) {

        RequestFuture<JSONObject> future = RequestFuture.newFuture();
       JsonObjectRequest stringRequest = new JsonObjectRequest(Request.Method.POST,p.URLS,params ,future,future);

        MySingleton.getInstance(c).addToRequestQueue(stringRequest);

        try {
              js = future.get(10, TimeUnit.SECONDS);
            Log.w("tag", "doInBackground: "+js);

            return js ;
        }catch (InterruptedException | ExecutionException e) {
           e.printStackTrace();
            e.getCause();
           return null;
        } catch (TimeoutException e) {
            e.printStackTrace();
            return null;
        }

    }
}
  

这是我得到的错误

  <?php
    if(isset($_POST))
     {  
       if(isset($_POST['username']) && isset($_POST['password']))
       {
        echo json_encode(array("username" => $_POST['username'], "password" => $_POST['password']));
       }else{json_encode(array("error" => "nothing set"));}
     }else{json_encode(array("error" => "nothing set"));}
    ?>

1 个答案:

答案 0 :(得分:0)

幸运的是我找到了解决方案,因为我很确定我不是唯一一个必须处理这样的事情的问题不在客户端,它是在服务器端我发送了一个带有帖子的对象所以它做了不能将对象识别为post变量,所以为了修复它你需要捕获那个被发送的对象并将其放入php变量中添加

$ data = json_decode(file_get_contents(&#39; php:// input&#39;),true);

到php脚本的顶部并使用$ data varable作为你的帖子数组expample $ data [&#39;用户名&#39;]以及另一件事,如果有人在此之前遇到过这个问题并查看过这篇文章并说什么都不知道我非常讨厌