import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
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.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import sereen.sql.Info;
import sereen.sql.InfoServicesNew;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class PenddingOrders extends Activity {
ArrayList<Info> info=new ArrayList<Info>();
int imgPendding=R.drawable.ex2;
ListView list;
ProgressDialog pd;
ArrayAdapter<String> adapter;
private String defValue = "N/A";
InfoServicesNew databaseHelper;
String name=InfoServicesNew.DB_TABLE_NAME;
static int img[]={R.drawable.ex2,R.drawable.ex2,R.drawable.ex2,R.drawable.ex2,
R.drawable.ex2,R.drawable.ex2,R.drawable.ex2,R.drawable.ex2, R.drawable.ex2,
R.drawable.ex2};
String data;
Intent o;
int position;
Object object;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pendding_orders);
databaseHelper = new InfoServicesNew(this);
list=(ListView)findViewById(R.id.listView1);
pd = new ProgressDialog(this);
new asy().execute("http://jsonblob.com/api/jsonBlob/53021f22e4b0f9ce1677329a");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.pendding_orders, menu);
return true;
}
public class asy extends AsyncTask<String, String, ArrayList<Info>>
{
@Override
protected ArrayList<Info> doInBackground(String... params) {
// TODO Auto-generated method stub
//activity is defined as a global variable in your AsyncTask
try {
HttpClient hc = new DefaultHttpClient();
HttpGet hg = new HttpGet(params[0]);
HttpResponse hr = hc.execute(hg);
HttpEntity he = hr.getEntity();
data = EntityUtils.toString(he);
Log.i("data", data);
}
catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
ArrayList<Info> sereenlist = new ArrayList<Info>();
sereenlist = getJSONData(data);
return sereenlist;
}
private ArrayList<Info> getJSONData(String data) {
// TODO Auto-generated method stub
ArrayList<Info> rs = null;
try {
JSONObject obj = new JSONObject(data);
JSONArray finalObj = obj.optJSONArray("orders");
for (int i = 0; i < finalObj.length(); i++)
{
final String orderNumber = finalObj.optJSONObject(i).optString(
"order-number");
final String orderAmount = finalObj.optJSONObject(i).optString(
"order-amount");
final String date = finalObj.optJSONObject(i).optString(
"date");
final String client = finalObj.optJSONObject(i).optString(
"client");
final String upperLimit = finalObj.optJSONObject(i).optString(
"upper-limit");
final String debt = finalObj.optJSONObject(i).optString(
"debt");
long id1=databaseHelper.insert(new Info(client,orderAmount,date ,orderNumber,upperLimit,debt));
// long id1 = databaseHelper.insert(info);
if(id1 < 0)
{
Toast.makeText(getApplicationContext(), "unsuccessfull add", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(), "done", Toast.LENGTH_LONG).show();
}
// select all
}
rs = databaseHelper.selectAll();
databaseHelper.close();
Log.i("size", finalObj.length()+"");
}//try end
catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return rs;
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pd.setTitle("fetching");
pd.setMessage("waiting...");
pd.show();
}
@Override
protected void onPostExecute(ArrayList<Info> result) {
// TODO Auto-generated method stub
SetAdapterList(result);
pd.dismiss();
}
private void SetAdapterList(ArrayList<Info> result)
{
// TODO Auto-generated method stu
CustomAdapter adapter=new CustomAdapter(getApplicationContext(),result);
list.setAdapter(adapter);
//
}
}
}
强文我想要做的是从链接中获取所有JSON数据..它显示在logcat中,所以我成功了...然后我尝试将其插入使用dbhelper和info类的数据库,其中包含所有值的getter和setter ..但每次我在这里运行代码我得到的东西:
02-19 01:09:34.490: E/AndroidRuntime(1210): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
答案 0 :(得分:0)
你是从工作线程调用的。您需要从主线程中调用Toast.makeText()(以及处理UI的大多数其他函数)。您可以使用处理程序,例如
activity.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(activity, "Hello", Toast.LENGTH_SHORT).show();
}
});
答案 1 :(得分:0)
首先,您必须从网络获取JSON数据并将其存储在结构中。 所以,第一步将是这样的。对于json解析我使用了Gson库。您还必须为将从请求中接收的对象创建模型类。
private void getJSONModelHttpRequest(){
AsyncHttpClient asyncClient = new AsyncHttpClient();
asyncClient.get(YOUR_URL_HERE , new AsyncHttpResponseHandler(){
@Override
public void onStart(){
}
@Override
public void onSuccess(String response){
try {
JSONObject jsonObj = new JSONObject(response);
Gson gson = new Gson();
ModelClass model = gson.fromJson(jsonObj.toString() , ModelClass.class);
// store the models in a array list or something like that to have the data available in the future
Log.d(“Model”, " model = " +teamModel.getAnAtttributeVal);
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Throwable e,String response){
}
@Override
public void onFinish(){
// Your database insertions here
}
});
}
答案 2 :(得分:0)
如果您使用处理程序的消息传递功能,也可以使用处理程序本身调用Toast。
我们所做的是使用线程内的sendMessage()方法传递您的字符串消息,
handler.sendMessage(msgObj);
和tehn使用,
在处理程序的handleMessage(Message msg)方法中获取消息String aResponse = msg.getData().getString("message");
完整示例here.
答案 3 :(得分:0)
就是这样的。首先创建一个处理程序并将数据发送给处理程序,无论你想要显示什么
if(id1 < 0)
{
Message msg=new Message();
msg.obj=unsuccessfull add;
handle.sendMessage(msg);
}
Handler handle=new Handler(){
public void handleMessage(Message msg) {
String data=(String)msg.obj;
Toast.makeText(activity, data, Toast.LENGTH_SHORT).show();
}
};
答案 4 :(得分:0)
我发现了问题..这是因为敬酒......我们无法在doInBackground服务中干杯..我的代码工作得很好..