JSONArray uploadArray = new JSONArray();
String URL = "http://your -url";
String upString=uploadArray.toString();
upString.replace("\\", "");
Log.e("Upload Request", upString);
String strinObjRecv = HttpClientNew.SendHttpPost(URL,
upString);
HttpClientNew.java
public class HttpClientNew {
private static final String TAG = "HttpClient";
public static String SendHttpPost(String URL, String jsonObjSendString) {
try {
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpPostRequest = new HttpPost(URL);
StringEntity se;
se = new StringEntity(jsonObjSendString);
Log.e("wtf1",EntityUtils.toString(se,HTTP.UTF_8));
se = new StringEntity(jsonObjSendString,HTTP.UTF_8);
Log.e("wtf2",EntityUtils.toString(se,HTTP.UTF_8));
// Set HTTP parameters
httpPostRequest.setEntity(se);
httpPostRequest.setHeader("Accept", "application/json");
httpPostRequest.setHeader("Content-type", "application/json");
//httpPostRequest.setHeader("Accept-Encoding", "gzip"); // only set this parameter if you would like to use gzip compression
long t = System.currentTimeMillis();
HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest);
Log.i(TAG, "HTTPResponse received in [" + (System.currentTimeMillis()-t) + "ms]");
// Get hold of the response entity (-> the data):
HttpEntity entity = response.getEntity();
if (entity != null) {
// Read the content stream
InputStream instream = entity.getContent();
Header contentEncoding = response.getFirstHeader("Content-Encoding");
if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
instream = new GZIPInputStream(instream);
}
// convert content stream to a String
String resultString= convertStreamToString(instream);
instream.close();
//resultString = resultString.substring(1,resultString.length()-1); // remove wrapping "[" and "]"
Log.i(TAG,"<Respose>\n"+resultString+"\n</JSONObject>\n"+response.getStatusLine().getStatusCode());
return resultString;
}
}
catch (Exception e)
{
// More about HTTP exception handling in another tutorial.
// For now we just print the stack trace.
e.printStackTrace();
}
return null;
}
private static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}