0

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

Here is what I am doing

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

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...

user3290805
  • 445
  • 2
  • 10
  • 28

2 Answers2

1

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.

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

Internet COnnection

I hope it would help.

Community
  • 1
  • 1
JNI_OnLoad
  • 5,472
  • 4
  • 35
  • 60
0

The link https://tt.student.com/back.json needs a certificate which should be trusted by android. Possibly you can accept the certificate by code but you would need that certificate. I tried to open in browser and it showed me untrusted certificate....

karan421
  • 863
  • 17
  • 43
  • I have added the method to get the certificate just not added in above code, in addition this is not correct url just example – user3290805 Oct 11 '14 at 11:28
  • Then you are not making the correct request... ask the guy who has made server in which way he needs the request.. i.e. what he needs in header and what should be the body.... you can see the request entity which you are posting on server... – karan421 Oct 11 '14 at 11:33
  • but when I sent request from httpclientin chrome with this 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 request, I get correct response – user3290805 Oct 11 '14 at 11:35
  • Is there any space in your request.. if there is replace it with %20 – karan421 Oct 11 '14 at 11:38
  • yes between dates I have space...yes you will get as this is not true url just an example – user3290805 Oct 11 '14 at 11:39
  • replace it with "%20" – karan421 Oct 11 '14 at 11:40
  • It still says Illegal character in query – user3290805 Oct 11 '14 at 11:51
  • could you show me the request that you are sending... after the change which i asked you to do.. – karan421 Oct 11 '14 at 11:53
  • https://tt.student.com/back.json?user=1&student_id=1&user_email=test@test.com&from=Fri%20Oct%2010 12:38:00%202014&20GMT+0200&to=Sat%20Oct%2011%2012:38:00%202014 GMT+0200&selector=Today – user3290805 Oct 11 '14 at 11:56
  • did you see any issue in it, as it says illegal character in url – user3290805 Oct 11 '14 at 12:00
  • i think it is breaking after october 10.. what is there after october 10 and before the time – karan421 Oct 11 '14 at 12:24