0

Can anyone help me or suggest a good example/ tutorial on how will I be able to post values to the website using JSON and httpost. What I am trying to do is to submit the values below to the website:

("DateObserved", _date);
("ProvinceId", "59");
("InformationTypeId", "3");
("ReportTypeId","3");
("IsCompetitor",_competitor);
("SubBrands","91");
("ConsumerSegments","3");

Its kinda hard for me as a newbie in android apply what I have read in different post in SO so I just wanna ask for a good example or tutorial that could help me understand it. anyways, I already tried something for this but I get ClientProtocolException. I really dont know where I missed something. Help me please...

HttpParams httpParameters = new BasicHttpParams();

        HttpConnectionParams.setConnectionTimeout(httpParameters, 15000);
        HttpConnectionParams.setSoTimeout(httpParameters, 15000);           

        HttpClient httpclient = new DefaultHttpClient(httpParameters);
        HttpPost httpPost = new HttpPost("http://svr4.sampleserver.com:0727/api/reports");



        JSONObject report = new JSONObject();
        report.put("IsCompetitor", _competitor);
        report.put("DateObserved", _date);
        report.put("ProvinceId", "59");
        report.put("InformationTypeId", "3");
        report.put("ReportTypeId","3");
        report.put("IsCompetitor",_competitor);
        report.put("SubBrands","91");
        report.put("ConsumerSegments","3");



         StringEntity entity1 = new StringEntity(report.toString(), HTTP.UTF_8);
         entity1.setContentType("application/json");
         httpPost.setEntity(entity1);

         httpPost.setHeader("Content-type", "application/json");
            HttpResponse response = httpclient.execute(httpPost);
            HttpEntity entity = response.getEntity();

         Log.e("ReportCode",_competitor); 
           // in.close();

    }catch (Exception e){
    Log.e("ClientServerDemo", "Error:", e);
    exception = e;
    //Toast.makeText(getApplicationContext(), e.getMessage().toString(), 5).show();
}

Logcat:

 08-01 18:56:29.668: E/ClientServerDemo(2283): org.apache.http.client.ClientProtocolException
 08-01 18:56:29.668: E/ClientServerDemo(2283):  at   org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:557)
 08-01 18:56:29.668: E/ClientServerDemo(2283):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
 08-01 18:56:29.668: E/ClientServerDemo(2283):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
08-01 18:56:29.668: E/ClientServerDemo(2283):   at com.undfgfgdfb.ireport.DoPost.doInBackground(DoPost.java:102)
08-01 18:56:29.668: E/ClientServerDemo(2283):   at com.undfgfgdfb.ireport.DoPost.doInBackground(DoPost.java:1)
08-01 18:56:29.668: E/ClientServerDemo(2283):   at android.os.AsyncTask$2.call(AsyncTask.java:252)
08-01 18:56:29.668: E/ClientServerDemo(2283):   at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
 08-01 18:56:29.668: E/ClientServerDemo(2283):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-01 18:56:29.668: E/ClientServerDemo(2283):   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081)
08-01 18:56:29.668: E/ClientServerDemo(2283):   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:574)
08-01 18:56:29.668: E/ClientServerDemo(2283):   at java.lang.Thread.run(Thread.java:1020)
08-01 18:56:29.668: E/ClientServerDemo(2283): Caused by: org.apache.http.auth.MalformedChallengeException: Authentication challenge is empty
08-01 18:56:29.668: E/ClientServerDemo(2283):   at org.apache.http.impl.auth.RFC2617Scheme.parseChallenge(RFC2617Scheme.java:72)

08-01 18:56:29.668: E/ClientServerDemo(2283): at org.apache.http.impl.auth.AuthSchemeBase.processChallenge(AuthSchemeBase.java:111) 08-01 18:56:29.668: E/ClientServerDemo(2283): at org.apache.http.impl.auth.BasicScheme.processChallenge(BasicScheme.java:95) 08-01 18:56:29.668: E/ClientServerDemo(2283): at org.apache.http.impl.client.DefaultRequestDirector.processChallenges(DefaultRequestDirector.java:1071) 08-01 18:56:29.668: E/ClientServerDemo(2283): at org.apache.http.impl.client.DefaultRequestDirector.handleResponse(DefaultRequestDirector.java:968) 08-01 18:56:29.668: E/ClientServerDemo(2283): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:478) 08-01 18:56:29.668: E/ClientServerDemo(2283): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) 08-01 18:56:29.668: E/ClientServerDemo(2283): ... 10 more

4

1 に答える 1