posting the data using httpput in android - android

I have a problem in posting the data to server by using httpput methods in android.I have to send feedback to server and getting json response. but i am getting 404 bad request. but i dont know where is the problem.
I am strucked here and didn't find any solution. Any suggestions?
My code is as follows:
HttpClient client = new DefaultHttpClient();
HttpPut put = new HttpPut(getString(R.string.feedBack));
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("userId", "8"));
pairs.add(new BasicNameValuePair("feedback",feedbackMessage
.getText().toString()));
put.addHeader("Content-Type", "application/json");
put.addHeader("Accept", "application/json");
put.setEntity(new UrlEncodedFormEntity(pairs));
HttpResponse response = client.execute(put);
Log.d(tag, "Result" + response.getStatusLine());

You are not initializing a URL object and passing in simple String.
You should do this instead:
URL url = new URL(getString(R.string.feedBack));
HttpClient client = new DefaultHttpClient();
HttpPut put= new HttpPut(url);
And it should work.

Related

Android send json to server

i write this code for send json to asp.net web page:
HttpClient httpclient = new DefaultHttpClient();
// 2. make POST request to the given URL
HttpPost httpPost = new HttpPost(url);
String json = "";
// 3. build jsonObject
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("name", myCountry.name);
// 4. convert JSONObject to JSON to String
json = jsonObject.toString();
// 5. set json to StringEntity
StringEntity se = new StringEntity(json);
// 6. set httpPost Entity
httpPost.setEntity(new UrlEncodedFormEntity(se,"UTF-8");
// 7. Set some headers to inform server about the type of the content
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json")
but in this line :
httpPost.setEntity(new UrlEncodedFormEntity(se,"UTF-8");
i get this error:
UrlEncodeFromEntity (java.util.List <? extends org.apache.http.NameValuePair>,String) in UrlEncodedFromEntity cannot be to (org.apache.http.entity.StringEntitym,String)
How can solve this?
Use httpPost.setEntity(new StringEntity(json.toString()));
instead of
httpPost.setEntity(new UrlEncodedFormEntity(se,"UTF-8");
Just try this line httpPost.setEntity(se);
-For arabic characters try Namevaluepairs
Example:-
ArrayList<NameValuePair> nameValuePairs1 = new ArrayList<NameValuePair>();
nameValuePairs1.add(new BasicNameValuePair("name", myCountry.name));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(URL);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs1));
Thanks!

Http GET Request Android

I know that it's possible to put some data in GET request body, but I know that it's not a good practice.
Though I want to do it but i don't know how. I searched the web but couldn't find anything.
Can you give me some little code examples or reference me to some usefull info.
Let's say we have this code, how to add nameValuePairs to request body of GET request :
String url = "something";
HttpClient httpClient = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(httpClient.getParams(),
TIMEOUT_MS);
HttpConnectionParams.setSoTimeout(httpClient.getParams(), TIMEOUT_MS);
HttpGet httpGet = new HttpGet(url);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("dtype", "0"));
nameValuePairs.add(new BasicNameValuePair("category_id", ""));
HttpResponse response = httpClient.execute(httpGet);
System.out.println("log in");
Thanks in advance.

How to post data having HTML content in query string

I need to post my string to server having html content,
Here is my code
Url Builder
Uri.Builder builder = new Uri.Builder()
.scheme("http")
.authority(domain)
.path(pageName)
.appendQueryParameter("myaction", "dopost")
.appendQueryParameter("mesg", msg.replace("\n","<br />"));
And the generated url looks like
domainname/pagename?myaction=dopost&mesg=Hello%2C%3Cbr%20%2F%3E%3Cbr%20%2F%3ETesting
execute http code
HttpGet getRequest = new HttpGet(url);
getRequest.setHeader(CONTENT_TYPE, MIME_TEXT_HTML); // html/text, utf-8
DefaultHttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(getRequest);
but this will not posted getting forbidden error after executing
Thank in advance
maybe a silly mistake, but a post usually is done with an HttpPost instead of an HttpGet
and looks like this:
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
}

The JSON object sent from android application is null when i want to access him in java servlet

I am writing an application in android and i am sending a json object to a java servlet.
This is the android code:
HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(URL);
JSONObject jsonObjectToPass = returnJsonStringObject();
List<NameValuePair> postParams = new ArrayList<NameValuePair>();
postParams.add(new BasicNameValuePair("jsonGPSParameter",jsonObjectToPass.toString()));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParams);
entity.setContentEncoding(HTTP.UTF_8);
entity.setContentType("application/json");
post.setHeader("Content-type", "application/json");
post.setEntity(entity);
response = httpClient.execute(post);
This is the java servlet code
request.getParameter("jsonGPSParameter");
The problem is that the getParameter() method return null.
Im not be able to spot the problem.
If someoane can help me it would be a great help.
Thanks
For the code to work proper i have to remove the next to lines from android code:
entity.setContentType("application/json");
post.setHeader("Content-type", "application/json");

HttpPost returns only part of response body

I'm performing login on server using HttpPost method.
CommonsConnection.initHttpConnection();
HttpResponse response;
String contentOfMyInputStream;
HttpGet initPostMethod = new HttpGet("https://stat.volia.com:8443/ktvinet/index.jsp");
response = CommonsConnection.hc.execute(initPostMethod);
HttpPost Statistics = new HttpPost("https://stat.volia.com:8443/ktvinet/j_security_check");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("j_username", "username"));
nameValuePairs.add(new BasicNameValuePair("j_password", "password"));
nameValuePairs.add(new BasicNameValuePair("submit", "%C2%EE%E9%F2%E8"));
Statistics.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = CommonsConnection.hc.execute(Statistics);
HttpGet mainPage = new HttpGet("https://stat.volia.com:8443/ktvinet/main.do");
response = CommonsConnection.hc.execute(mainPage);
contentOfMyInputStream = EntityUtils.toString(response.getEntity());
The response I get - body of webpage - is like 1/5 part of webpage, which ends with "...".
Am I missing some parameter for httpclient ?
How are you examining the response? if you're looking int he debugger or something similar, it may well truncate long strings, try dumping it in a file.
Logcat displays only part of long string response.

Categories

Resources