0
I have a problem when it’s time to connect, someone could help me please. I’m new and I’m not able to make the connection of JSON PARSE HTTP with FRAGMENTS all tutorials show with ACTIVITY, but I’ve tried to convert and I can’t, I’m grateful, if you can help me.
Jsonparser.java code
package br.com.menuruenf.menuruenf.domain;
import android.util.Log;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
/**
* Created by Rodolfo on 15/09/2015.
*/
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
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 + "n");
}
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;
}
}
Dinnerfragment.java code
package br.com.menuruenf.menuruenf.fragments;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Text;
import java.io.IOException;
import br.com.menuruenf.menuruenf.R;
import br.com.menuruenf.menuruenf.domain.Dinner;
import br.com.menuruenf.menuruenf.domain.JSONParser;
/**
* Created by Rodolfo on 01/09/2015.
*/
public class DinnerFragment extends BaseFragment {
//URL to get JSON Array
private static String url = "http://menu-api.herokuapp.com/dinners.json";
//JSON Node data
private static final String TAG_DINNER = "";
private static final String TAG_ID = "id";
private static final String TAG_DATE = "date";
private static final String TAG_SALAD = "salad";
private static final String TAG_ACCOMPANIMENT = "accompaniment";
private static final String TAG_GARNISH = "garnish";
private static final String TAG_PROTEIN1 = "protein1";
private static final String TAG_PROTEIN2 = "protein2";
private static final String TAG_PROTEIN3 = "protein3";
private static final String TAG_DESSERT = "dessert";
private static final String TAG_JUICE = "juice";
JSONArray dinner = null;
TextView dinner_id;
TextView dinner_date;
TextView dinner_salad;
TextView dinner_accompaniment1;
TextView dinner_garnish;
TextView dinner_protein1;
TextView dinner_protein2;
TextView dinner_protein3;
TextView dinner_dessert;
TextView dinner_juice;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment_dinner, container, false);
//Import TextView
dinner_date = (TextView) view.findViewById(R.id.dinner_date);
dinner_salad = (TextView) view.findViewById(R.id.dinner_salad);
dinner_accompaniment1 = (TextView) view.findViewById(R.id.dinner_accompaniment1);
dinner_garnish = (TextView) view.findViewById(R.id.dinner_garnish);
dinner_protein1 = (TextView) view.findViewById(R.id.dinner_protein1);
dinner_protein2 = (TextView) view.findViewById(R.id.dinner_protein2);
dinner_protein3 = (TextView) view.findViewById(R.id.dinner_protein3);
dinner_dessert = (TextView) view.findViewById(R.id.dinner_dessert);
dinner_juice = (TextView) view.findViewById(R.id.dinner_juice);
//Creating new JSON Parser
JSONParser jsonParser = new JSONParser();
//Getting JSON from URL
JSONObject json = jsonParser.getJSONFromUrl(url);
try {
//Getting JSON Array
dinner = json.getJSONArray(TAG_DINNER);
JSONObject c = dinner.getJSONObject(0);
//Storing JSON item in a Variable
String did = c.getString(TAG_ID);
String ddate = c.getString(TAG_DATE);
String dsalad = c.getString(TAG_SALAD);
String daccompaniment = c.getString(TAG_ACCOMPANIMENT);
String dgarnish = c.getString(TAG_GARNISH);
String dprotein1 = c.getString(TAG_PROTEIN1);
String dprotein2 = c.getString(TAG_PROTEIN2);
String dprotein3 = c.getString(TAG_PROTEIN3);
String ddessert = c.getString(TAG_DESSERT);
String djuice = c.getString(TAG_JUICE);
//Set JSON DATA in TextView
dinner_date.setText(ddate);
dinner_salad.setText(dsalad);
dinner_accompaniment1.setText(daccompaniment);
dinner_garnish.setText(dgarnish);
dinner_protein1.setText(dprotein1);
dinner_protein2.setText(dprotein2);
dinner_protein3.setText(dprotein3);
dinner_dessert.setText(ddessert);
dinner_juice.setText(djuice);
}catch (JSONException e){
e.printStackTrace();
}
return view;
}
}
UPDATE A Great solution is the retrofit, simple and functional.
Rodolfo, this is a very common problem in Android development and requires a good change in the way you are developing. Going straight to the point, you cannot make network requests or disk access (usually blocking operations) on
MainThread
. I recommend taking a look at the concept of Threading. The simplest would be to use aAsyncTask
which is an abstraction layer on a Thread. But beyond that, I recommend seeing the effects of usingAsyncTasks
. Here at Stackoverflow you have several questions aboutAsyncTask
.– Wakim
Good afternoon, so I implemented with Asynctasks only that use Fragment and not activiy e to with a lot of problem when turning Activity to Fragment activityAtual.this example
– Rodolfo Peixoto
An easy way to access your context
Fragment
I recommend thegetContext
if using theSupport Library v4
or elsegetActivity
otherwise.– Wakim
I used the retrofit to make my life better.
– Rodolfo Peixoto