在Android中编码URL后,json没有从服务器接收

时间:2015-02-25 11:16:02

标签: java javascript android json

我遇到了麻烦。我花了8个小时,但失败了。我希望使用电子邮件作为标准从服务器接收json。以下是java脚本中代码的一部分:http://jsfiddle.net/7ydnw3ty/1/ 当我尝试在android中转换它时,我得到空的结果。请告诉我,问题在哪里。拜托,请让我摆脱困境。

以下是正在调用的代码段。

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);

        nameValuePairs.add(new BasicNameValuePair("authtoken", "61d40d3b0395f730d8e24e77cfada8d7"));
        nameValuePairs.add(new BasicNameValuePair("zc_ownername","raffbr2"));
        nameValuePairs.add(new BasicNameValuePair("criteria", "'Email==\"teste1@gmail.com\"'"));

        nameValuePairs.add(new BasicNameValuePair("scope","creatorapi"));

        pBar = new ProgressDialog(MainActivity.this);
        pBar.setCanceledOnTouchOutside(false);
        pBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        pBar.setMessage("Communication With Server, Registering...");
        pBar.show();

        MyAsyncTask1 mTask = new MyAsyncTask1();
        mTask.setOnResultsListener(MainActivity.this);
        mTask.execute(getResources().getString(R.string.readserverUrl)+"LoginSecurity1"
                , nameValuePairs);**

异步任务fot后台处理

    public class MyAsyncTask1 extends AsyncTask<Object, Integer, String> {

    ResultsListener listener;


    public void setOnResultsListener(ResultsListener listener) {
        this.listener = listener;
    }

    @Override
    protected String doInBackground(Object... params) {
        // TODO Auto-generated method stub
        String res = null;
        DefaultHttpClient responseBody = null;
        if (params.length > 0) {
            String mUrl =  params[0].toString();
            List<NameValuePair> mParams = (List<NameValuePair>) params[1];


            try{




                DefaultHttpClient httpclient= new DefaultHttpClient();              

                HttpPost httpget = new HttpPost(mUrl);

                 httpget.setEntity(new UrlEncodedFormEntity(mParams, "UTF-8"));


                HttpResponse response = httpclient.execute(httpget);

                BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                String line = "";
                while ((line = in.readLine()) != null) {
                    res=line; 
                    System.out.println(line);
                }
                in.close();

                }catch(Exception e){

                    return res=e.toString();

                }

            }
        return res;
    }
    @Override
    protected void onPostExecute(String result) {
    //Toast.makeText(getBaseContext(), result, Toast.LENGTH_SHORT).show();
        if (!listener.equals(null)) {
            listener.onResultsSucceeded(result);
        }
    }

    public HttpClient getNewHttpClient() {
        try {
            KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
            trustStore.load(null, null);



            HttpParams params = new BasicHttpParams();
            HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
            HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

            SchemeRegistry registry = new SchemeRegistry();
            registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));


            ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);

            return new DefaultHttpClient(ccm, params);
        } catch (Exception e) {
            return new DefaultHttpClient();
        }
    }


}

1 个答案:

答案 0 :(得分:0)

我建议使用Volley lib来解决JSON POST或GET任务: Volley example