Hi I have php code to encode an array and I want my android app to receive the array and store to the local variable
php code on server side:
echo json_encode(array('result'=>$result));
how to make android to receive the array from server. I'm thinking of using Volley libray but dont know how to implement
should be something like:
ArrayList eventdetail = repond(url...)
I Believe you need time to research. Seems like you have just started.
Their are lot of ways to do this.
You can use Rest API's or SOAP API's whichever you feel like. or You can build your own http client on android to request and recieve data from web server.
all the best.
For SOAP you can use Ksoap2
For implementing Volley this is the best source.
use below code to parse json array
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(url);
JSONArray jArrayVersion = json.getJSONArray("result");
for (int j = 0; j < jArrayVersion.length(); j++) {
JSONObject items = jArrayVersion.getJSONObject(j);
String namesv = items.getString("name");
String message = items.getString("message");
}
//JSONParser class
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
// Making HTTP request
try {
// defaultHttpClient
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
System.out.println("url " + url);
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
// The default value is zero, that means the timeout is not used.
int timeoutSocket = 6000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
System.out.println("url " + url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
System.out.println("url0 " + url);
HttpResponse httpResponse = httpclient.execute(httpPost);
System.out.println("url1 " + url);
HttpEntity httpEntity = httpResponse.getEntity();
System.out.println("url2 " + url);
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "
");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}