发送使用异步任务的android参数JSON请求 [英] sending json request with parameters using async task android

查看:146
本文介绍了发送使用异步任务的android参数JSON请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图发送一个JSON请求到服务器的一些参数。请求会和异步任务工作正常,但它在服务器上抛出异常,说无效的网址

I am trying to send a json request to server with some parameters. The request is going and async task is working fine but it throws exception at server and says invalid url

下面是我在做什么。

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity);   

    Button btnChart = (Button) findViewById(R.id.btn_chart);

    // Defining click event listener for the button btn_chart
    OnClickListener clickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            new HttpAsyncTask().execute("https://tt.student.com/back.json");
              }
    };


    // Setting event click listener for the button btn_chart of the MainActivity layout
    btnChart.setOnClickListener(clickListener);
 }

public static String POST(String url){
    InputStream inputStream = null;
    String result = "";
    try {

        // 1. create HttpClient
        HttpClient httpclient = getNewHttpClient();

        // 2. make POST request to the given URL
        HttpPost httpPost = new HttpPost(url);

        String json = "";

        // 3. build jsonObject
        JSONObject jsonObject = new JSONObject();
        jsonObject.accumulate("user", 1);
        jsonObject.accumulate("student_id", 1);
        jsonObject.accumulate("user_email", "test@test.com");
        jsonObject.accumulate("from", "Fri Oct 10 12:38:00 2014 GMT+0200");
        jsonObject.accumulate("to", "Sat Oct 11 12:38:00 2014 GMT+0200");

        // 4. convert JSONObject to JSON to String
        json = jsonObject.toString();

        // ** Alternative way to convert Person object to JSON string usin Jackson Lib
        // ObjectMapper mapper = new ObjectMapper();
        // json = mapper.writeValueAsString(person);

        // 5. set json to StringEntity
        StringEntity se = new StringEntity(json);

        // 6. set httpPost Entity
        httpPost.setEntity(se);

        // 7. Set some headers to inform server about the type of the content
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-type", "application/json");

        // 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{
    BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
    String line = "";
    String result = "";
    while((line = bufferedReader.readLine()) != null)
        result += line;

    inputStream.close();
    return result;

}
private class HttpAsyncTask extends AsyncTask<String, Void, String> {
    @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(), "Received!", Toast.LENGTH_LONG).show();
        Log.d(TAG,result);
    }
}

在网址我都尝试的方法之一是上面code等是通过在URL中的参数 https://tt.student.com/back.json?user=1& ; student_id数据= 1&安培; user=testh@test.com&从周五= 10月10日12点38分00秒2014年GMT + 0200&安培;到周六= 10月11日12点38分00秒2014年GMT + 0200
这是说,在URL非法字符...

In the url I tried both way one is in above code and other is to pass parameter in url itself https://tt.student.com/back.json?user=1&student_id=1&user=testh@test.com&from=Fri Oct 10 12:38:00 2014 GMT+0200&to=Sat Oct 11 12:38:00 2014 GMT+0200 for this it says illegal character in url...

推荐答案

我想,这个问题可能是与服务器或无线哀告状态。您的设备只能连接到无线网络,但没有通过的真实性从服务器访问Internet或技术上更,交换数据包。

I think, the issue could be with the server or Wifi Supplicate state. your device is only connected to wifi but have not passed the authenticity to access the internet or more technically, to exchange packets from server.

我会建议你使用的方法来检查设备的连通性,我认为这是与你的情况,因为我有类似的状态,我花了差不多1-2个小时,使其工作。
对于这里的互联网连接检查链接

I would recommend you to use method to check device connectivity, I think it is the case with you as I had the similar state and I spent almost 1-2 hours to make it work. FOr internet connectivity check here is the link

互联网连接

我希望这会有所帮助。

这篇关于发送使用异步任务的android参数JSON请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆