Click here to Skip to main content
15,881,938 members
Home / Discussions / Android
   

Android

 
AnswerRe: Alarm Manager Pin
David Crow30-Jan-17 16:50
David Crow30-Jan-17 16:50 
GeneralRe: Alarm Manager Pin
Pavlex431-Jan-17 10:48
Pavlex431-Jan-17 10:48 
SuggestionRe: Alarm Manager Pin
David Crow31-Jan-17 17:23
David Crow31-Jan-17 17:23 
GeneralRe: Alarm Manager Pin
Pavlex431-Jan-17 20:22
Pavlex431-Jan-17 20:22 
QuestionRe: Alarm Manager Pin
David Crow1-Feb-17 2:14
David Crow1-Feb-17 2:14 
QuestionAndroid OTG Detect App Pin
Pavlex427-Jan-17 10:54
Pavlex427-Jan-17 10:54 
AnswerRe: Android OTG Detect App Pin
Richard MacCutchan27-Jan-17 22:05
mveRichard MacCutchan27-Jan-17 22:05 
QuestionSimple HTTP Post Request - application/json Pin
Django_Untaken27-Jan-17 9:21
Django_Untaken27-Jan-17 9:21 
Hello there. I am trying to make a simple http POST request using HttpUrlConnection. Problem is that response code is 200 but the response message NOT success.

You can see that I set all the necessary header parameters. I am supposed to get success message. But I get {"status":false,"message":"Invalid access"} with responseCode = 200.

Below is the code (I am developing against Lollipop)

Java
protected SubmitRequestTask.ResponseObject doInBackground(Object... object)
{
	String encodedStr = URLEncoder.encode(object.getDataToSendRAW(), "UTF-8");

	BufferedReader reader = null;
        int responseCode = -1;
	try
        {
            URL url = new URL(/*My SERVER URL*/);  //Converting address String to URL

	      Map<String, String> headerParams = new HashMap<String, String>();
	      headerParams.put("Accept", "application/json");
            headerParams.put("Content-Type", "application/json");

            HttpUrlConnection networkConnection = MakeRequest(
                    url,
                    "POST",
                    headerParams,
                    null);

            if(networkConnection == null)
                return new ResponseObject(-1, "Uknown Error");

            StringBuilder sb = new StringBuilder();
            reader = new BufferedReader(new InputStreamReader(networkConnection.getInputStream()));

            while ((response = reader.readLine()) != null)
                sb.append(response + "\n");

            response = sb.toString();
	}
	catch(Exception ex) {}
        finally { 
            responseCode = networkConnection.getResponseCode();
            networkConnection.disconnect();
        }

return new ResponseObject(responseCode, response);
}

private HttpUrlConnection MakeRequest(URL url, String method, Map<String, String> headerParams, NetworkConfig config)
{
	HttpURLConnection htp_url_connection = (HttpURLConnection) url.openConnection();
	htp_url_connection.setRequestMethod(method);
    
        if (headerParams != null && method.trim().toLowerCase().equals("post"))
        {
                htp_url_connection.setDoOutput(true);

                for (Map.Entry<String, String> entry : headerParams.entrySet()) {
                   (htp_url_connection.setRequestProperty(entry.getKey(), entry.getValue());
                }

                if (encodedStr.trim().length() > 0) {
                    OutputStreamWriter writer = new OutputStreamWriter(htp_url_connection.getOutputStream());
                    writer.write(encodedStr);
                    writer.flush();
                }
	}

	htp_url_connection.setConnectTimeout(5000);
	int responseCode = htp_url_connection.getResponseCode();

        if(responseCode == HttpURLConnection.HTTP_OK /*200*/)
                return htp_url_connection;
}

Sample JSON data (which I encode in doInBackground).
HTML
{
"plot_unique_id":"355618",
"plot_physical_address":"owner",
"plot_id":"355617",
"location":"31.475885,74.3422574",
"action_type":"1",
"plot_sub_id":"1",
"apk_version":"1.0",
"imei_no":"862037029044152",
"owners":[......]
}


What could be the problem?? Thanks for any input.
AnswerRe: Simple HTTP Post Request - application/json Pin
Richard MacCutchan27-Jan-17 22:03
mveRichard MacCutchan27-Jan-17 22:03 
QuestionAndroid USB Detection Pin
Pavlex421-Jan-17 11:18
Pavlex421-Jan-17 11:18 
AnswerRe: Android USB Detection Pin
Afzaal Ahmad Zeeshan21-Jan-17 12:28
professionalAfzaal Ahmad Zeeshan21-Jan-17 12:28 
GeneralRe: Android USB Detection Pin
Pavlex421-Jan-17 12:53
Pavlex421-Jan-17 12:53 
GeneralRe: Android USB Detection Pin
Afzaal Ahmad Zeeshan21-Jan-17 13:00
professionalAfzaal Ahmad Zeeshan21-Jan-17 13:00 
GeneralRe: Android USB Detection Pin
Pavlex421-Jan-17 13:08
Pavlex421-Jan-17 13:08 
GeneralRe: Android USB Detection Pin
Afzaal Ahmad Zeeshan21-Jan-17 13:12
professionalAfzaal Ahmad Zeeshan21-Jan-17 13:12 
GeneralRe: Android USB Detection Pin
Pavlex421-Jan-17 13:36
Pavlex421-Jan-17 13:36 
GeneralRe: Android USB Detection Pin
David Crow24-Jan-17 2:38
David Crow24-Jan-17 2:38 
GeneralRe: Android USB Detection Pin
Jochen Arndt24-Jan-17 3:34
professionalJochen Arndt24-Jan-17 3:34 
GeneralRe: Android USB Detection Pin
Pavlex424-Jan-17 3:41
Pavlex424-Jan-17 3:41 
GeneralRe: Android USB Detection Pin
Jochen Arndt24-Jan-17 3:53
professionalJochen Arndt24-Jan-17 3:53 
GeneralRe: Android USB Detection Pin
Pavlex424-Jan-17 3:54
Pavlex424-Jan-17 3:54 
GeneralRe: Android USB Detection Pin
Jochen Arndt24-Jan-17 4:05
professionalJochen Arndt24-Jan-17 4:05 
GeneralRe: Android USB Detection Pin
Pavlex424-Jan-17 4:07
Pavlex424-Jan-17 4:07 
GeneralRe: Android USB Detection Pin
Jochen Arndt24-Jan-17 4:23
professionalJochen Arndt24-Jan-17 4:23 
GeneralRe: Android USB Detection Pin
Pavlex424-Jan-17 4:42
Pavlex424-Jan-17 4:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.