Sending multipart POST data without external libraries - java

My Android app architecture is based on DefaultHttpClient. About 2 days ago, I header they're improving HttpURLConnection and I was about to change the whole architecture to work with it, but it seemed like too much code rewriting, so I sticked to DefaultHttpClient.
Now I'm having to upload a multipart entity and I have been reading about adding an external library called mime from Apache, which happens to be deprecated.
My question would be, is there a way to send multipart entity using the Android or Java SDK? Cause if there isn't then I suppose I'm gonna change it all to HttpURLConnection

You could try out some classes that I wrote for my projects. The multipart form class is a pretty trivial implementation, but it should be easily hackable to accommodate sending arrays of data or whatever you need.
/**
* Used to provide an interface for sending post data of various types.
*/
public interface PostData {
/**
* Add the specified field to the post data.
*
* #param name The name or title of the field.
* #param value The value of the field.
* #return This object (for streamed programming)
*/
public PostData addField(String name, String value);
/**
* End the data.
*/
public PostData end();
public String getContentType();
public String toString();
public int getLength();
}
/**
* Generate a multi-part form to post data.
*/
public static class MultiPartForm implements PostData {
private final static String boundary = "AaBbC0xQrpqqqqqqqq";
private StringBuilder sb = new StringBuilder();
public PostData addField(String name, String value) {
sb.append("--")
.append(boundary)
.append("\nContent-Disposition: form-data; name='")
.append(name)
.append("'\n\n")
.append(value)
.append("\n");
return this;
}
public PostData end() {
sb.append("--").append(boundary).append("--");
return this;
}
public final String getContentType() {
return "multipart/form-data;boundary=" + boundary;
}
public String toString() {
return sb.toString();
}
public int getLength() {
return sb.length();
}
}
/**
* Creates URL encoded data.
* Does not include the question mark at the beginning of the string.
*/
public static class UrlEncodedData implements PostData {
private StringBuilder sb = new StringBuilder();
public PostData addField(String name, String value) {
if (sb.length() > 0) {
sb.append("&");
}
try {
sb.append(URLEncoder.encode(name, "UTF-8"))
.append("=")
.append(URLEncoder.encode(value, "UTF-8"));
// Shouldn't ever happen.
} catch (java.io.UnsupportedEncodingException e) {}
return this;
}
public PostData end() {
return this;
}
public final String getContentType() {
return "application/x-www-form-urlencoded";
}
public String toString() {
return sb.toString();
}
public int getLength() {
return sb.length();
}
}
Along with these, I also created a method to make retrieving data more simple as well. I use HttpURLConnection with this method, but it should be pretty simple to get the gist of how to do the same thing with the DefaultHttpClient. Note that it is synchronous, so you will have to call it in another thread:
/**
* Loads a specified URL with the params incoded in the post.
*
* #throws Exception For numerous reasons.
* #param url The base URL to connect to.
* #param data A PostData object to fetch. Specifiy null for a GET request.
* #return The result.
*/
public static String loadUrl(final String url, final PostData data)
throws Exception {
HttpURLConnection urlConnection = null;
BufferedReader br = null;
final StringBuilder result = new StringBuilder();
try {
urlConnection = (HttpURLConnection)new URL(url).openConnection();
if (data != null) {
urlConnection.setRequestMethod("POST");;
urlConnection.setRequestProperty("Content-Type", data.getContentType());
urlConnection.setRequestProperty("Content-Length",
Integer.toString(data.getLength()));
urlConnection.setDoOutput(true);
DataOutputStream dos = new DataOutputStream(
urlConnection.getOutputStream());
dos.writeBytes(data.toString());
dos.flush();
dos.close();
urlConnection.connect();
}
br = new BufferedReader(
new InputStreamReader(urlConnection.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
result.append(line);
}
} finally {
try { urlConnection.disconnect(); } catch (Exception e) {}
try { br.close(); } catch (Exception e) {}
}
return result.toString();
}
Usage is quite trivial:
Utils.loadUrl("someApi.com/foobar/"
new Utils.MultiPartForm()
.addField("first_name", "John")
.addField("last_name", "Doe")
.end());

Related

Data fetching from an API in android

I have tried to fetch data from an API which has a key. But in the output it says "app key not found".
I have tested it on Postman and it works properly.
Here is my code:
public class fetchData extends AsyncTask<Void,Void,Void> {
String data="";
#Override
protected Void doInBackground(Void... voids) {
try {
URL url=new URL("https://app.inyek.com/app_api/api_extra/all_order.php?");
HttpURLConnection con=(HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded/json;charset=UTF-8");
con.setRequestProperty("app_key","whatever");
con.setDoOutput(true);
}
I strongly suggest you make an Abstract HttpRequestTask which extends AsyncTask. In this abstract ancestor you can make some helper methods for reading your input, something like so:
/**
* HttpRequestTask is an abstract extension of an AsyncTask for HTTP Requests.
*
* #param <P>
* Type for parameter(s) to doInBackground (can be Void if none provided)
* #param <R>
* Type for result of request (can be Void if ignored, or using listeners.)
*/
public abstract class HttpRequestTask<P, R> extends AsyncTask<P, Integer, R>
{
private static final String TAG = "HttpRequestTask";
// Post form encoded requests, get back JSON response
private static final RequestMethod DEFAULT_REQUEST_METHOD = RequestMethod.POST;
private static final String DEFAULT_CONTENT_TYPE = "application/x-www-form-urlencoded;charset=UTF-8;";
private static final String DEFAULT_ACCEPT = "application/json;";
private static final int DEFAULT_TIMEOUT = 8000; // 8 seconds
private static final String CHARSET = "UTF-8";
protected static final String NULL_CONTEXT = "Context is null.";
protected static final String INVALID_RESPONSE = "The server did not send back a valid response.";
// Request methods supported by back-end
protected enum RequestMethod
{
GET("GET"),
POST("POST");
private final String method;
RequestMethod(String method)
{
this.method = method;
}
#Override
public String toString()
{
return this.method;
}
}
/**
* ALWAYS use application context here to prevent memory leaks.
*
*/
protected HttpRequestTask(#NonNull final Context context)
{
this.context = context;
}
protected void verifyConnection() throws IOException
{
if (!SystemUtil.isInternetAvailable(context))
{
throw new IOException("Internet is unavailable.");
}
}
/**
* Creates and opens a URLConnection for the url parameter, as well as setting request options.
*
* #param url
* to connect to.
*
* #return opened HTTPURLConnection for POSTing data to ctservices.
*/
protected HttpURLConnection getURLConnection(URL url) throws IOException
{
return this.getURLConnection(url, DEFAULT_REQUEST_METHOD, DEFAULT_CONTENT_TYPE,
DEFAULT_ACCEPT, DEFAULT_TIMEOUT);
}
/**
* Creates and opens a URLConnection for the url parameter, as well as setting request options.
*
* #param url
* to connect to.
*
* #return opened HTTPURLConnection
*/
protected HttpURLConnection getURLConnection(#NonNull final URL url,
#NonNull final RequestMethod requestMethod,
#NonNull final String contentType,
#Nullable final String accept, final int timeout)
throws IOException
{
verifyConnection();
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod(requestMethod.toString());
urlConnection.setRequestProperty("Content-Type", contentType);
if (accept != null && !accept.isEmpty())
{
urlConnection.setRequestProperty("Accept", accept);
}
urlConnection.setReadTimeout(timeout);
urlConnection.setConnectTimeout(timeout);
urlConnection.setUseCaches(false);
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
return urlConnection;
}
/**
* Creates and opens a URLConnection for the url parameter, but does not set any request options.
*
* #param url
* to connect to.
*
* #return opened HTTPURLConnection without parameters set.
*/
protected HttpURLConnection getBasicURLConnection(URL url) throws IOException
{
if (!SystemUtil.isInternetAvailable(applicationContext.get()))
{
throw new IOException("Internet is unavailable.");
}
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
return urlConnection;
}
/**
* Write a JSONObject of request parameters to the output stream as form-encoded data.
*
* #param urlConnection
* opened urlConnection with output enabled (done by getURLConnection).
* #param params
* to write to request.
*
* #throws IOException
* problem writing to output stream
*/
protected void writeParams(HttpURLConnection urlConnection, JSONObject params) throws IOException
{
OutputStream outputStream = urlConnection.getOutputStream();
BufferedWriter outWriter = new BufferedWriter(new OutputStreamWriter(outputStream,
StandardCharsets.UTF_8));
String urlParams = this.encodeJSONObject(params);
outWriter.write(urlParams);
outWriter.flush();
outWriter.close();
outputStream.close();
}
/**
* Reads the response of a URLConnection from the input stream and puts it in a string.
*
* #param urlConnection
* opened urlConnection with input enabled (done by getURLConnection).
*
* #return response string
*
* #throws IOException
* problem reading input stream
*/
protected String readResponse(HttpURLConnection urlConnection) throws IOException
{
InputStream inputStream = null;
try
{
/* If we failed to connect will throw a SocketResponseTimeoutException,
* which is an IOException. */
int responseCode = urlConnection.getResponseCode();
if (HttpURLConnection.HTTP_OK != responseCode)
{
throw new IOException("Bad response code - " + responseCode);
}
inputStream = urlConnection.getInputStream();
final String response = parseInputStream(inputStream);
urlConnection.disconnect();
return response;
}
finally
{
if (inputStream != null)
{
try
{
inputStream.close();
}
catch (Exception e) {}
}
}
}
protected Context getContext()
{
return this.context;
}
protected String getString(final int resId)
{
return getContext().getString(resId);
}
/**
* Encodes a JSONObject as a form-data URL string.
*
* #param jo
* to encode
*
* #return encoded URL string
*/
private String encodeJSONObject(JSONObject jo)
{
StringBuilder sb = new StringBuilder();
boolean first = true;
Iterator<String> itr = jo.keys();
String key;
Object val;
try
{
while (itr.hasNext())
{
key = itr.next();
val = jo.get(key);
if (first)
{
first = false;
}
else
{
sb.append('&');
}
sb.append(URLEncoder.encode(key, CHARSET));
sb.append('=');
sb.append(URLEncoder.encode(val.toString(), CHARSET));
}
}
catch (JSONException | UnsupportedEncodingException e) {}
return sb.toString();
}
private String parseInputStream(InputStream is) throws IOException
{
BufferedReader br = null;
try
{
br = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null)
{
sb.append(line);
}
return sb.toString();
}
finally
{
if (br != null)
{
try
{
br.close();
}
catch (Exception e) {}
}
}
}
/**
* Merges any properties of b into a that don't already have a key match in a.
*
* #param a
* merging to
* #param b
* merging from
*
* #return a with any unique values from b
*/
protected JSONObject mergeJSONObjects(JSONObject a, JSONObject b)
{
if (b == null)
{
return a;
}
if (a == null)
{
return b;
}
try
{
Iterator<String> bItr = b.keys();
String key;
while (bItr.hasNext())
{
key = bItr.next();
if (!a.has(key))
{
a.put(key, b.get(key));
}
}
return a;
}
catch (Exception ex)
{
Log.e(TAG, ex.getClass().getSimpleName() + " in mergeJSONObjects: " + ex.getMessage() +
'\n' + Log.getStackTraceString(ex));
return a;
}
}
}
Then you can extend your HttpRequestTask to easily make network requests:
public class ExampleNetworkTask extends HttpRequestTask<Void, Void>
{
private static final String TAG = "ExampleNetworkTask";
private final SimpleListener successListener;
private final StringListener errorListener;
private final String servicesUrl;
public static void start(#NonNull final Context context,
#Nullable final SimpleListener successListener,
#Nullable final StringListener errorListener)
throws IllegalArgumentException
{
if (context == null)
{
throw new IllegalArgumentException(NULL_CONTEXT);
}
new ExampleNetworkTask(context, successListener, errorListener).execute();
}
private ExampleNetworkTask(#NonNull final Context context,
#Nullable final SimpleListener successListener,
#Nullable final StringListener errorListener)
{
super(context);
this.servicesUrl = SystemUtil.getServiceUrl(getContext(), R.string.example_service);
this.successListener = successListener;
this.errorListener = errorListener;
}
#Override
protected Void doInBackground(Void... voids)
{
try
{
final HttpURLConnection urlConnection = super.getURLConnection(new URL(servicesUrl));
final JSONObject params = new JSONObject();
// Add params
params.put("app_key", appKey);
params.put("order_number", orderNumber);
// ...
// Send request, read parse response
super.writeParams(urlConnection, params);
final String response = super.readResponse(urlConnection);
final JSONObject responseObj = new JSONObject(response);
// Handle response
}
catch (Exception ex)
{
final String msg = ex.getLocalizedMessage();
Log.e(TAG, ex.getClass().getSimpleName() + ": " + msg + '\n' +
Log.getStackTraceString(ex));
// Handle network exceptions and other exceptions here.
}
return null;
}
}
In PostMan, how did you specify the app key? was it through an HTTP header?
(Sorry, I would have added a comment, but I do not have enough reputation)
Or was it specified as a GET parameter?
In the latter case, try something like:
URL url=new URL("https://app.inyek.com/app_api/api_extra/all_order.php?app_key=YOUR_KEY");
Welcome to Stack Exchange! Firstly I'd suggest you don't put your API Key within questions and/or images, as they might be sensitive and can be abused by malicious users. Feel free to edit your question and remove them.
To answer your query, I think you need to write the contents to the http request body in a json format. This can be done as per the guide on the following webpage:
https://www.baeldung.com/httpurlconnection-post
In summary, you need to create an output stream and write the contents to it directly.
Thank you guys! finally i got the answer using OkHttpClient. here is the code:
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody body = RequestBody.create(mediaType, "app_key=whatever");
Request request = new Request.Builder()
.url("https://app.inyek.com/app_api/api_extra/all_order.php")
.post(body)
.addHeader("Content-Type", "application/x-www-form-urlencoded")
.addHeader("key", "whatever")
.addHeader("cache-control", "no-cache")
.addHeader("Postman-Token", "whatever")
.build();
Response response = client.newCall(request).execute();

How to manage multiple AsyncTasks in Android

I want to create my AsyncTask in separated classes because of I have to parse XML and it's a bunch of code.
So I want to get noticed in the activity everytime an asynctask is completed.
I followed this question
The problem is that when I have to make multiple petitions, everytime one is completed call the same method.
I want to call different methods depending of the AsyncTask Class I called.
EDIT: One of my AsyncTask Classes
public class FichaProducto extends AsyncTask<Void,Void,String>{
private String codigo, descripcion, ubicacion, descPromocion, currentUser,ip,port,codigoBuscar, respuesta;
private float precio;
private static final String TAG = "Logger";
private OnTaskCompleted listener;
/**
* Without listener
* #param codigoBuscar
* #param currentUser
* #param ip
* #param port
*/
public FichaProducto(String codigoBuscar,String currentUser,String ip,String port) {
setCodigoBuscar(codigoBuscar);
setCurrentUser(currentUser);
setIp(ip);
setPort(port);
}
/**
* With listener
* #param codigoBuscar
* #param currentUser
* #param ip
* #param port
* #param listener
*/
public FichaProducto(String codigoBuscar, String currentUser, String ip, String port, OnTaskCompleted listener) {
setCodigoBuscar(codigoBuscar);
setCurrentUser(currentUser);
setIp(ip);
setPort(port);
this.listener = listener;
}
/**
* set the xml response
* #param response
*/
public void setRespuesta(String respuesta) {
this.respuesta = respuesta;
}
/**
* #return server xml response
*/
#Override
protected String doInBackground(Void... params) {
StringBuilder respuesta = new StringBuilder();
URL url;
HttpURLConnection conexion = null;
try{
//Create the connection and set parameters
url = new URL("http://"+getIp()+":"+getPort());
Log.d(TAG,url.toString());
conexion = (HttpURLConnection)url.openConnection();
conexion.setRequestMethod("POST");
conexion.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conexion.setRequestProperty("Content-Length", "" + Integer.toString(getXML().getBytes().length));
conexion.setRequestProperty("Content-Language", "es-ES");
conexion.setUseCaches(false);
conexion.setDoInput(true);
conexion.setDoOutput(true);
//Send the petition
DataOutputStream dos = null;
dos = new DataOutputStream(conexion.getOutputStream());
dos.writeBytes(getXML());
dos.flush();
dos.close();
//Get the response
InputStream is = conexion.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String linea;
while ((linea = br.readLine()) != null){
respuesta.append(linea);
respuesta.append("\n");
}
br.close();
Log.d(TAG,"From asynctask the response is: "+respuesta.toString());
return respuesta.toString();
}catch(MalformedURLException e){
Log.e(TAG,"MalformedURLException in AsyncTask "+e.getMessage());
return null;
}catch (IOException e){
Log.e(TAG,"IO Exception in AsyncTask "+e.getMessage());
return null;
} finally {
//Close the connection
if (conexion != null){
conexion.disconnect();
}
}
}
/**
* Set the response and call the listener
*/
#Override
protected void onPostExecute(String respuesta){
setRespuesta(respuesta);
//Here it'll read the xml received and will set the variables
if (listener != null){
listener.onTaskCompleted();
}
}
(If it's bad, excuse my english pls)
To check which task is compelte you can use follow:
Update OnTaskCompleted with onTaskCompleted(int id);
private OnTaskCompleted listener;
private int id;
public FichaProducto(String codigoBuscar, String currentUser, String ip,
String port, OnTaskCompleted listener, int id) {
setCodigoBuscar(codigoBuscar);
setCurrentUser(currentUser);
setIp(ip);
setPort(port);
this.listener = listener;
this.id = id
}
#Override
protected void onPostExecute(String respuesta){
setRespuesta(respuesta);
//Here it'll read the xml received and will set the variables
if (listener != null){
listener.onTaskCompleted(id);
}
}
In activity class you can use something like this:
private final int PARSE_XML = 0
private final int PARSE_JSON = 1
void parseXml(){
new FichaProducto(codigoBuscar, currentUser, ip, port, PARSE_XML)
}
#Override
onTaskCompleted(int id){
switch (id){
case PARSE_XML:
break;
case PARSE_JSON:
break;
}
}

Error when sending a POST request, authenticated by OAuth, using signpost

I'm trying to send aמ HTTP POST request in order to send contacts information to a Mail Exchange Server, using their API (creating a new "subscriber"). I'm using Java and java.util.HttpURLConnection.
When I try signing the connection, I'm getting a null reference exception. If I try signing the connection prior to adding the setRequestProperty headers, I'm getting an Invalid Signature response from the server.
Using a GET request with the same general procedure works - which means, as far as I understand, that my signing method (and key values etc.) is OK.
The service I'm trying to use has some kind of a "SDK" available, written in .NET. I didn't try to use it but I do believe it to work (they declare so).
I tried to replicate their procedure. Below you can find my code, follow by theirs:
private static HttpURLConnection createAndSendOAuthPostRequestWithParams () throws MalformedURLException, IOException, Exception {
String url = "http://apisdomain/v1.0/lists/354467/subscribers";
// Here I set up the values given by the provider (API's admin) which I removed from the example
String clientKey = "";
String clientSecret = "";
String userKey = "";
String userSecret = "";
String postData = "NAME=TestSubscriber&EMAIL=test#gmail.com
byte[] postBody = postData.getBytes("UTF-8");
URL apiUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) apiUrl.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("content-length", String.valueOf(postBody.length));
connection.setRequestProperty("content-type", "application/x-www-form-urlencoded; charset=UTF-8");
//OAuth
OAuthConsumer consumer = new DefaultOAuthConsumer (clientKey, clientSecret);
//consumer.setAdditionalParameters(parameters);
consumer.setTokenWithSecret(userKey, userSecret);
HttpRequest httpReq = consumer.sign(connection); //Where the exception occurs
if (!postBody.toString().isEmpty()) {
connection.setDoOutput(true);
try (DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream())) {
outputStream.write(postBody);
outputStream.flush();
}
}
return connection;
}
From thier SDK:
using System.Text;
namespace ResponderSDK
{
using OAuth;
using System;
using System.Collections.Generic;
using System.Net;
using System.IO;
class ResponderOAuth
{
/* Contains the last HTTP status code returned. */
public HttpStatusCode http_code;
/* Contains the last API call. */
public string url;
/* Set up the API root URL. */
public string host = "http://api.responder.co.il/v1.0/";
/* Set timeout default. */
public int timeout = 3000;
/* Set connect timeout. */
public int connect_timeout = 30;
/* Verify SSL Cert. */
public bool ssl_verifypeer = false;
/* Response format. */
public string format = "json";
/* Contains the last HTTP headers returned. */
public string http_info;
/* Set the useragent. */
public string useragent = "ResponderOAuth v0.1-beta";
/*debug info*/
public string headers_string;
public string base_string;
public string post_string;
/* Signature */
private OAuthSignatureMethod_HMAC_SHA1 signature;
/* OAuthConsumer */
private OAuthConsumer consumer;
/* Token */
private OAuthToken token;
public ResponderOAuth(string consumer_key, string consumer_secret, string oauth_token = null, string oauth_token_secret = null)
{
this.signature = new OAuthSignatureMethod_HMAC_SHA1();
this.consumer = new OAuthConsumer(consumer_key, consumer_secret);
if (!String.IsNullOrEmpty(oauth_token) && !String.IsNullOrEmpty(oauth_token_secret))
{
this.token = new OAuthToken(oauth_token, oauth_token_secret);
}
else
{
this.token = null;
}
}
public string http_request(string url, string method = "GET", ParametersArray parameters = null)
{
method = method.ToUpper();
if (url.LastIndexOf("https://") != 0 && url.LastIndexOf("http://") != 0)
{
url = String.Format("{0}{1}", this.host, url);
}
if (method.Equals("GET"))
parameters = null;
OAuthRequest request = OAuthRequest.from_consumer_and_token(this.consumer, this.token, method, url, parameters);
request.sign_request(this.signature, this.consumer, this.token);
this.base_string = request.base_string;
if (method.Equals("GET"))
return this.http(request.to_url(), "GET", request.to_header(), null);
else
return this.http(request.get_normalized_http_url(), method, request.to_header(), request.to_postdata());
}
private string http(string url, string method, WebHeaderCollection headers, string data = null)
{
List<string> new_http_info = new List<string>();
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
HttpWebRequest request = null;
if (!method.Equals("DELETE"))
request = (HttpWebRequest)WebRequest.Create(url);
else
{
if (!String.IsNullOrEmpty(data))
{
url = String.Format("{0}?{1}", url, data);
}
request = (HttpWebRequest)WebRequest.Create(url);
}
/* WebRequest settings */
((HttpWebRequest)request).ProtocolVersion = System.Net.HttpVersion.Version10;
((HttpWebRequest)request).UserAgent = this.useragent;
((HttpWebRequest)request).ContinueTimeout = this.connect_timeout;
((HttpWebRequest)request).Timeout = this.timeout;
((HttpWebRequest)request).Headers = headers;
((HttpWebRequest)request).UseDefaultCredentials = true;
((HttpWebRequest)request).PreAuthenticate = true;
((HttpWebRequest)request).Credentials = CredentialCache.DefaultCredentials;
this.headers_string = headers.ToString();
this.post_string = data;
byte[] dataByteArray = null;
if ((!String.IsNullOrEmpty(data) && method.Equals("POST")) || method.Equals("PUT"))
{
((HttpWebRequest)request).ContentType = "application/x-www-form-urlencoded";
System.Text.Encoding encoding = System.Text.Encoding.UTF8;
dataByteArray = encoding.GetBytes(data);
((HttpWebRequest)request).ContentLength = dataByteArray.Length;
((HttpWebRequest)request).Expect = "";
}
switch (method)
{
case "POST":
((HttpWebRequest)request).Method = "POST";
if (!String.IsNullOrEmpty(data))
{
Stream dataPost = request.GetRequestStream();
dataPost.Write(dataByteArray, 0, dataByteArray.Length);
dataPost.Close();
}
break;
case "PUT":
((HttpWebRequest)request).Method = "PUT";
if (!String.IsNullOrEmpty(data))
{
Stream dataPost = request.GetRequestStream();
dataPost.Write(dataByteArray, 0, dataByteArray.Length);
dataPost.Close();
}
break;
case "DELETE":
((HttpWebRequest)request).Method = "DELETE";
break;
}
WebResponse response = request.GetResponse();
this.http_code = ((HttpWebResponse)response).StatusCode;
// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
return reader.ReadToEnd();
}
}
}
If your input String format is json, you can change content-type to "application/json" and try signing in after adding the setRequestProperty headers.

JSON parse error in php that produced in by jsonSerialize()

I use below code to produce JSON string and parse it in php. I create an instace of this class and call setData method
//#PART1
String s = "{\"Category\":";
List<CategoryModel> pmo = new ArrayList<CategoryModel>();
pmo = CategoryModule.getAllRecords(c);
s += new JSONSerializer().serialize(pmo);
s += ",\"Data\":";
//#PART2
List<DataModel> amo = new ArrayList<DataModel>();
amo = DataModule.getAllRecords(c);
s += new JSONSerializer().serialize(amo);
s += "}";
I decode the result by below code
$categories = json_decode($data)->{'Category'};
$datas = json_decode($data)->{'Data'};
$username = "kkk";
foreach($categories as $category){
$id = $category->{'id'};
$name = $category->{'name'};
$sql = "INSERT INTO category (id,name,username) VALUES ('$id','$name','$username')";
$link->query($sql);
}
foreach($datas as $data){
$id = $data->{'id'};
$text = $data->{'text'};
$date = $data->{'date'};
$sql = "INSERT INTO data (id,text,date,username) VALUES ('$id','$name','$date','$username')";
$link->query($sql);
}
When I just use #PART1 to produce json string in the php code the decoding occurs in success. But when I add #PART2 to JSON string no one of them decodes successfully. I guess the problem can be from java code.
Please guide me
JSON result is http://aiga.ir/webservice/datastore/a.txt
I use this code for sending data
package ir.aiga.apps.network;
public class WebServiceComm extends AsyncTask<String, Void, Void> {
// Required initialization
private String Content;
private String Error = null;
private ProgressDialog Dialog;
private String data ="";
private boolean visible=true;
private InterFace doInPreExecute=new InterFace() {
#Override
public void doSomething() {
// TODO Auto-generated method stub
}
#Override
public void getResult(String output) {
// TODO Auto-generated method stub
}
#Override
public void getJSONArray(JSONArray array) {
// TODO Auto-generated method stub
}
};
private InterFace doInPostExecute=new InterFace() {
#Override
public void doSomething() {
// TODO Auto-generated method stub
}
#Override
public void getResult(String output) {
// TODO Auto-generated method stub
}
#Override
public void getJSONArray(JSONArray array) {
// TODO Auto-generated method stub
}
};
public WebServiceComm(Context context,String title,String text){
try {
data +="&" + URLEncoder.encode("data", "UTF-8") + "=";
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Dialog=new ProgressDialog(context,ProgressDialog.STYLE_SPINNER);
Dialog.setTitle(title);
Dialog.setMessage(text);
}
public WebServiceComm(){
try {
data +="&" + URLEncoder.encode("data", "UTF-8") + "=";
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void setData(String data){
this.data+=data;
}
protected void onPreExecute() {
// NOTE: You can call UI Element here.
if(visible)
Dialog.show();
}
// Call after onPreExecute method
protected Void doInBackground(String... urls) {
/************ Make Post Call To Web Server ***********/
BufferedReader reader=null;
// Send data
try
{
// Defined URL where to send data
URL url = new URL(urls[0]);
// Send POST data request
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write( data );
wr.flush();
// Get the server response
reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
// Read Server Response
while((line = reader.readLine()) != null)
{
// Append server response in string
sb.append(line + "");
}
// Append Server Response To Content String
Content = sb.toString();
}
catch(Exception ex)
{
Error = ex.getMessage();
ex.printStackTrace();
}
finally
{
try
{
reader.close();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
/*****************************************************/
return null;
}
protected void onPostExecute(Void unused) {
// NOTE: You can call UI Element here.
if (Error != null) {
} else {
// Show Response Json On Screen (activity)
/****************** Start Parse Response JSON Data *************/
JSONObject jsonResponse;
try {
/****** Creates a new JSONObject with name/value mappings from the JSON string. ********/
jsonResponse = new JSONObject(Content);
/***** Returns the value mapped by name if it exists and is a JSONArray. ***/
/******* Returns null otherwise. *******/
JSONArray jsonMainNode = jsonResponse.optJSONArray("Android");
doInPostExecute.getJSONArray(jsonMainNode);
doInPostExecute.doSomething();
if(visible)
Dialog.dismiss();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* #return the doInPreExecute
*/
public InterFace getDoInPreExecute() {
return doInPreExecute;
}
/**
* #param doInPreExecute the doInPreExecute to set
*/
public void setDoInPreExecute(InterFace doInPreExecute) {
this.doInPreExecute = doInPreExecute;
}
/**
* #return the doInPostExecute
*/
public InterFace getDoInPostExecute() {
return doInPostExecute;
}
/**
* #param doInPostExecute the doInPostExecute to set
*/
public void setDoInPostExecute(InterFace doInPostExecute) {
this.doInPostExecute = doInPostExecute;
}
/**
* #return the visible
*/
public boolean isVisible() {
return visible;
}
/**
* #param visible the visible to set
*/
public void setVisible(boolean visible) {
this.visible = visible;
}
}
please try to use this code for sending and receiving JSON with utf-8 encoding:
try {
URL url = new URL("your url");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(
conn.getOutputStream(), "UTF-8");
String request = "your json";
writer.write(request);
writer.flush();
System.out.println("Code:" + conn.getResponseCode());
System.out.println("mess:" + conn.getResponseMessage());
String response = "";
BufferedReader reader = new BufferedReader(new InputStreamReader(
conn.getInputStream(), "UTF-8"));
String line;
while ((line = reader.readLine()) != null) {
response += line;
}
System.out.println(new String(response.getBytes(), "UTF8"));
writer.close();
reader.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
It's very bad to manually build JSON manually.
Using a JSON Mapper library
public class MyCustomModel {
public List<CategoryModel> Category;
public List<DataModel> Data;
public class CategoryModel{
//code here
}
public class DataModel{
//code here
}
}
Then use it GSON to serialize that object into JSON
MyCustomModel customModel = new MyCustomModel();
//populate object
//......
Gson gson = new Gson();
String json = gson.toJson(customModel);
Using standard library
JSONArray categoryArr = new JSONArray();
List<CategoryModel> categories = CategoryModule.getAllRecords(c);
for (CategoryModel category : categories) {
JSONObject categoryObj = new JSONObject();
categoryObj.put("class", category.getClass());
categoryObj.put("id", category.getId());
categoryObj.put("name", category.getName());
categoryArr.put(categoryObj);
}
Then do the same with the other list then combine both array
JSONObject jObject = new JSONObject();
jObject.put("Category", categoryArr);
jObject.put("Data", dataArr);

Applet php communication using POST

so here is my problem. I simply can't get my applet&php communication going. I'm using the below class for communication
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.Vector;
public class POST {
private String postParameters = "";
private String webPage;
private Vector<String>names;
private Vector<String>values;
public POST(){
values = new Vector<String>();
names = new Vector<String>();
}
/**
* Adds a post variable (page.php?name=value)
*
* #param name the variable name
* #param value the variable value, can be set to null, the url will simply become &name instead of &name=value
* null
*/
public void addPostValue(String name, String value) throws UnsupportedEncodingException {
if (value == null) {
try {
postParameters += "&" + URLEncoder.encode(name, "UTF-8");
names.add(name);
values.add("");
} catch (Exception ex) {
ex.printStackTrace();
}
} else {
postParameters += "&" + URLEncoder.encode(name, "UTF-8") + "=" + URLEncoder.encode(value, "UTF-8");
names.add(name);
values.add(value);
}
}
/**
* Send post data without waiting for site output
*
* #return true if sending data terminated succesfully
*/
public boolean sendPost() {
try {
if (webPage == null || webPage.equals("")) {
throw new Exception("Empty url");
}
URL url = new URL(webPage);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(postParameters);
wr.flush();
} catch (Exception e) {
e.printStackTrace();
return false;
}
postParameters = "";
return true;
}
/**
* Sends data, then waits for site output
*
* #return null if no data is received, or a String containing the data
*/
public String sendPostWithReturnValue() {
String returnValue = "";
try {
if (webPage == null || webPage.equals("")) {
throw new Exception("Empty url");
}
URL url = new URL(webPage);
URLConnection conn =
url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr =
new OutputStreamWriter(conn.getOutputStream());
wr.write(postParameters);
wr.flush();
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
returnValue += line + "\n";
}
wr.close();
rd.close();
} catch (Exception e) {
e.printStackTrace();
return null;
}
postParameters = "";
values = null;
names=null;
values = new Vector<String>();
names = new Vector<String>();
return returnValue;
}
/**
* Sets the page to point at for sending post variables
*
* #param webPageToPointAt the page that will receive your post data
*/
public void setWebPageToPointAt(String webPageToPointAt) {
webPage = webPageToPointAt;
}
/**
* #returns A Nx2 matrix containing all parameters name and values
*/
public String[][] getParameters() {
String[][] str = new String[names.size()][2];
for (int i = 0; i < str.length; i++) {
str[i][0] = names.get(i);
str[i][1] = values.get(i);
}
return str;
}
}
And this is the function within my applet that is calling it
public void postajRezultat(int brojP, int brojH) throws IOException{
P = Integer.toString(brojP);
H = Integer.toString(brojH);
POST post = new POST();
post.addPostValue("brojH", H);
post.addPostValue("brojP", P);
post.addPostValue("ime", ime);
post.setWebPageToPointAt(getCodeBase().toString() + "/includes/save.php");
post.sendPost();
And last this is the simple php script that should show the results of POST. Please help me, I've tried everything and i won't work...The error php gives me is "Undefined index "ime", "brojP", "brojH".
<?php
mysql_connect ("127.0.0.1","root","vertrigo");
mysql_select_db ("projekt_db");
$ime=$_POST['ime'];
$brojP=$_POST['brojP'];
$brojH=$_POST['brojH'];
echo("Test");
echo($brojP . "" . $ime . "" . $brojH);
$a=mysql_query("INSERT INTO highscore ('id', 'ime', 'brojP', 'brojH') VALUES (NULL, '" . $ime . "'," . $brojP . "," . $brojH . ")");
?>
Why don't you use some kind of framework for HTTP communication?
In my experience Apache HTTP Client is excelent solution for such operations, it makes request very easy to implement eg.
HttpPost post=new HttpPost("where_to_send_post_request_url")
post.setEntity(createdURLEncodedEntity) // here you add your post parameters as entity
response=client.execute(post); // execute your post
String page=EntityUtils.toString(response.getEntity()); // get response as string content eg html
EntityUtils.consume(response.getEntity()); // release connection etc.
isn't that simple? You don't have to reinvent the wheel again:)

Categories