Issue
I'm trying to fetch data from a server using this url www.example.com/directory/ but it's not working ... it came up with this that json is not sending any action to server... so all I want is to make the url this way-- www.example.com/directory/index.php?action=searchFood&money=50
This is my JsonFetcher.java
public class JsonFetcher extends AsyncTask<Pair<String, String>, Integer, JSONObject> {
private JSONObject jsonObject;
public AsyncResponse delegate = null;
public interface AsyncResponse {
void processFinish(JSONObject output);
}
public JsonFetcher(AsyncResponse delegate) {
this.delegate = delegate;
}
@Override
protected void onPostExecute(JSONObject jsonObject) {
super.onPostExecute(jsonObject);
delegate.processFinish(jsonObject);
}
@Override
protected JSONObject doInBackground(final Pair<String, String>... params) {
int count = params.length;
URL url = null;
int responseCode = 0;
jsonObject = null;
try {
url = new URL("http://www.example.com/directory/");
} catch (MalformedURLException e) {
e.printStackTrace();
}
HttpURLConnection conn = null;
try {
conn = (HttpURLConnection) url.openConnection();
//conn.connect();
} catch (IOException e) {
e.printStackTrace();
}
try {
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
Uri.Builder builder = new Uri.Builder();
Date date = new Date();
String md5 = date.toString();
//Log.e("json lock",lock);
//Log.e("json key",md5);
for (int i = 0; i < count; i++) {
builder.appendQueryParameter(String.valueOf(params[i].first),String.valueOf(params[i].second));
}
String query = builder.build().getEncodedQuery();
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(query);
writer.flush();
writer.close();
os.close();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// read the response
try {
responseCode = conn.getResponseCode();
} catch (IOException e) {
e.printStackTrace();
}
if (responseCode == HttpsURLConnection.HTTP_OK) {
InputStream in = null;
try {
in = new BufferedInputStream(conn.getInputStream());
} catch (IOException e) {
e.printStackTrace();
} finally {
//conn.disconnect();
}
BufferedReader r = new BufferedReader(new InputStreamReader(in));
StringBuilder total = new StringBuilder();
String line;
try {
while ((line = r.readLine()) != null) {
total.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
Log.e("json", total.toString());
try {
jsonObject = new JSONObject(total.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
return jsonObject;
}
}
Is there any possibility to write in this way
url = new URL("http://www.example/directory/index.php?action= PairActionName & PairMoney" );
here PairActionName and PairMoney will be receiving from MainActivity
Solution
Since you are using AsyncTask for performing server operations, you shall create a encoded query string to pass arguments to the url, and write it to the URLconnection object. The example below will clear what I just said:
Uri.Builder builder = new Uri.Builder().appendQueryParameter("param1", value1)
.appendQueryParameter("param2", value2);
final String query = builder.build().getEncodedQuery();
URL url;
HttpURLConnection connection = null;
try{
url = new URL("http://www.example.com/directory/");
connection = (HttpURLConnection) url.openConnection();
OutputStream os = connection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(query);
writer.flush();
writer.close();
os.close();
connection.connect();
}
On the server side you can simply retrieve the parameter values (value1) using the parameter name (param1).
Also, you can add as much parameters as you need using .appendQueryParameter()
method.
Answered By - myrahKaur
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.