android helper android helper - 6 months ago 17
JSON Question

Could anyone guide me on this? I'm trying to parse some objects into my JSONArray

Could anyone guide me on this? I'm trying to parse some objects into my JSONArray. But my JSON file is a bit complicated. I'm trying to get the logo, name and the hadtip

here is my json file

{"status": 200,"status_message":"Success","response":
[{"platform":0,"logo":"http:\/\/betfan.com\/images\/servicelogo\/258.png",
"name":"Andy Bell Racing","oppid":"258","hadtip":0},
{"platform":0,"logo":"http:\/\/betfan.com\/images\/servicelogo\/327.png",
"name":"Dog Race Wagers","oppid":"327","hadtip":0},
{"platform":2,"logo":"http:\/\/betkudos.com\/images\/servicelogo\/4.png",
"name":"Free Tip - Bet Kudos","oppid":"4","hadtip":0},
{"platform":7,"logo":"http:\/\/tipstertv.com\/images\/servicelogo\/1.png",
"name":"Free Tip - Tipster TV","oppid":"1","hadtip":0},
{"platform":1,"logo":"http:\/\/winninginformationnetwork.com\/images\/servicelogo\/6.png",
"name":"Free Tip - WIN","oppid":"6","hadtip":0},
{"platform":2,"logo":"http:\/\/betkudos.com\/images\/servicelogo\/34.png",
"name":"Lucky 7 }]}


I tried much but could not get solve.

and here is my java code in android

please anyone help me how i do right

this is my custom adapter code

package com.example.newapp;

import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class Custom_adapter extends BaseAdapter{
Context context;
ArrayList<list>list;
public Custom_adapter(Context context, ArrayList<list> mylist) {
super();
this.context = context;
this.list = mylist;
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView==null){
LayoutInflater inflater=(LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);

convertView=inflater.inflate(R.layout.activity_custom_adapter,null);
ImageView icon=(ImageView)convertView.findViewById(R.id.iconID);
TextView title=(TextView) convertView.findViewById(R.id.txt_titleID);
TextView tips=(TextView) convertView.findViewById(R.id.txt_tipsID);


list List=list.get(position);
icon.setImageResource(List.getIcon());
title.setText(List.getTitle());
tips.setText(List.getTips());

}
return convertView;
}
}


and here is my asynctask code

package com.example.newapp;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
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.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class Welcome extends Activity {

String[] temp_Arr;
ListView lv;
ArrayList<list>listitem;
String title;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);

login();

title="Free Tip - Betfans";

lv=(ListView) findViewById(R.id.lvID);
listitem=new ArrayList<list>();
}
private void login() {

class LoginAsync extends AsyncTask<String, Void, String>{

private Dialog loadingDialog;

@Override
protected void onPreExecute() {
super.onPreExecute();
loadingDialog = ProgressDialog.show(Welcome .this, "Please while wait", "Loading...");
}

@Override
protected String doInBackground(String... params) {



JSONObject jsonObject = new JSONObject();

try {
jsonObject.accumulate("device", "de3b5e2d7f401829ea54fd5aca9d2fa92deb65c1b9b6b7729b3c3c8df11e75f4");

} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

try {
jsonObject.accumulate("userID", "2s385");
} catch (JSONException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
jsonObject.accumulate("userkey", "d69b87b2c0ad828abf7c0ac30b83ea0c11");
} catch (JSONException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}

try {
jsonObject.accumulate("key", "MEu07MgiuWgXwJOo7aOe1aHL0yM8VvP");
} catch (JSONException e4) {
// TODO Auto-generated catch block
e4.printStackTrace();
}
// 4. convert JSONObject to JSON to String
String dataString = jsonObject.toString();

InputStream is = null;
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("data", dataString));

String result = null;

try{


HttpClient httpClient = new DefaultHttpClient();
HttpGet httpRequest = new HttpGet();

URI apiurl = new URI("http://mobile.betfan.com/api/?action=v2list&device=de3b5e2d7f401829ea54fd5aca9d2fa92deb65c1b9b6b7729b3c3c8df11e75f4&key=MEu07MgiuWgXwJOo7Oe1aHL0yM8VvP&userID=2385&userkey=d69b87b2c0ad828bf7c0c30b83ea0c11");

httpRequest.setURI(apiurl);

HttpResponse response = httpClient.execute(httpRequest);

HttpEntity entity = response.getEntity();

is = entity.getContent();

BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();

String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}

protected void onPostExecute(String result){
String s = result.trim();
//Toast.makeText(Welcome.this, s, Toast.LENGTH_LONG).show();
Toast.makeText(Welcome.this, "Services received!", Toast.LENGTH_LONG).show();

loadingDialog.dismiss();

JSONObject respObject;
try {
respObject = new JSONObject(s);
String active = respObject.getString("status_message");

//boolean status = respObject.getBoolean("status_message");

if(active.equalsIgnoreCase("success")){
JSONArray array = respObject.getJSONArray("response");
for (int i =0; i<array.length();i++){

JSONObject jsonObject = array.getJSONObject(i);
String logo= jsonObject.getString("logo");
String name= jsonObject.getString("name");
String hadtip = jsonObject.getString("hadtip");
String tipstr="";
if(hadtip.equalsIgnoreCase("1")){
tipstr = "Tips Are ready.";
}else{
tipstr = "No Tips Today"+hadtip;
}

listitem.add(new list(1,name, tipstr));

}
lv.setAdapter(new Custom_adapter(Welcome.this, listitem));

}else {
Toast.makeText(getApplicationContext(), "services received Fail", Toast.LENGTH_LONG).show();

}

} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

lv.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String strName = listitem.get(arg2).getTitle();

Toast.makeText(Welcome.this, strName, Toast.LENGTH_LONG)
.show();

}
});

}

}

LoginAsync la = new LoginAsync();
la.execute();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}


here is my getter setter list...

package com.example.newapp;

public class list {
private int icon;
private String title;
private String tips;


public int getIcon() {
return icon;
}
public void setIcon(int icon) {
this.icon = icon;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getTips() {
return tips;
}
public void setTips(String tips) {
this.tips = tips;
}
public list(int icon, String title, String tips) {
super();
this.icon = icon;
this.title = title;
this.tips = tips;
}


}

Answer

just add in your adapter

           String logo= jsonObject.getString("logo");