利用线程获取json数据,肯定能获取到,获取到的json传给公有的json,可是每次公有的json里面都是空值,求大神给解决方案。
我的代码
package textview.exam;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import com.baidu.apistore.sdk.ApiCallBack;
import com.baidu.apistore.sdk.ApiStoreSDK;
import com.baidu.apistore.sdk.network.Parameters;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class FragmentMainchatweather extends Fragment {
TextView mTextView;
Button test;
String city;
WeatherPopup weatherPopup;
JSONArray forecast;
ListView list;
JSONObject retData=null;
private int[] weatherzt = new int[]
{ R.drawable.sun , R.drawable.cloudy
, R.drawable.baoyu , R.drawable.thouder,R.drawable.snow,R.drawable.zhongyu,
R.drawable.zhenyu,R.drawable.dayu};
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view=inflater.inflate(R.layout.main_chat_weather, null);
Button weatheradd=(Button)view.findViewById(R.id.weatheradd);
list = (ListView) view.findViewById(R.id.chat_weathers);
list.setOnItemClickListener(weatherforListener);
weatheradd.setOnClickListener(weatheraddListener);
return view;
}
//点击添加按钮弹出输入城市对话框,然后根据城市名字进行查询并且显示
View.OnClickListener weatheraddListener=new View.OnClickListener() {
@Override
public void onClick(View v) {
//加载文本框布局
final View weatheradd=getActivity().getLayoutInflater().inflate(R.layout.weatheradd, null);
new AlertDialog.Builder(getActivity())
.setView(weatheradd)
.setPositiveButton("确定",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
TextView city1=(TextView)weatheradd.findViewById(R.id.citytext);
city=city1.getText().toString();
Log.i("我靠"+retData,"1");
SyncThread st1 = new SyncThread();
st1.start();
Log.i("我靠"+retData,"2");
//view更新
weatherselect();
}
})
.setNegativeButton("取消",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.create()
.show();
}
};
AdapterView.OnItemClickListener weatherforListener=new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//为未来天气设置SimpleAdapter
//final SimpleAdapter simpleAdapter1 = new SimpleAdapter(getActivity(), listItemx, R.layout.weather_popup_item,
// new String[]{"forecastweek", "forecasttype", "forecastlowtemp", "forecasthightemp"},
// new int[]{R.id.forecastweek, R.id.forecasttype, R.id.forecastlowtemp, R.id.forecasthightemp});
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//实例化SelectPicPopupWindow
weatherPopup = new WeatherPopup(getActivity());
//weatherPopup.setAdapter(simpleAdapter1);
//显示窗口
weatherPopup.showAtLocation(getActivity().findViewById(R.id.main), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
}
});
}
};
public void weatherselect(){
List<Map<String, Object>> listItemx = new ArrayList<Map<String, Object>>();
try {
Map<String, Object> listItem = new HashMap<String, Object>();
JSONObject today = retData.getJSONObject("today");
listItem.put("city", retData.getString("city"));
listItem.put("lowtemp", today.getString("lowtemp"));
listItem.put("hightemp", today.getString("hightemp"));
listItem.put("week", today.getString("week"));
listItem.put("curTemp", today.getString("curTemp"));
listItem.put("type", today.getString("type"));
listItem.put("fengli", today.getString("fengli"));
listItem.put("aqi", today.getString("aqi"));
listItemx.add(listItem);
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getActivity(),"数据加载错误", Toast.LENGTH_LONG).show();
}
final SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity(), listItemx, R.layout.weather_simple_item,
new String[]{"city", "lowtemp", "hightemp", "week", "curTemp", "type", "fengli", "aqi"},
new int[]{R.id.city, R.id.lowtemp, R.id.hightemp, R.id.week, R.id.curTemp, R.id.type, R.id.fengli, R.id.aqi})
{
@Override
public View getView(int position, View convertView,ViewGroup parent){
final View view=super.getView(position, convertView, parent);
TextView text=(TextView)view.findViewById(R.id.type);
switch (text.getText().toString()){
case "多云":
view.setBackgroundResource(weatherzt[1]);
break;
case "雷阵雨":
view.setBackgroundResource(weatherzt[3]);
break;
case "阵雨":
view.setBackgroundResource(weatherzt[6]);
break;
case "大雨":
view.setBackgroundResource(weatherzt[7]);
break;
case "暴雨":
view.setBackgroundResource(weatherzt[2]);
break;
case "晴":
view.setBackgroundResource(weatherzt[0]);
break;
case "雪":
view.setBackgroundResource(weatherzt[4]);
break;
case "中雨":
view.setBackgroundResource(weatherzt[5]);
break;
default:
view.setBackgroundResource(weatherzt[0]);
break;
}
return view;
}
};
simpleAdapter.notifyDataSetChanged();
list.setAdapter(simpleAdapter);
}
//开启子线程得到数据
public class SyncThread extends Thread {
@Override
public void run() {
Parameters para = new Parameters();
para.put("cityname", city);
ApiStoreSDK.execute("http://apis.baidu.com/apistore/weatherservice/recentweathers",
ApiStoreSDK.GET,
para,
new ApiCallBack() {
@Override
public void onSuccess(int status, String responseString) {
Log.i("连接状态(天气)", "成功");
try {
JSONObject dataJson = new JSONObject(responseString);
retData = dataJson.getJSONObject("retData");
Log.i("我靠" + retData, "3");
} catch (JSONException e) {
Toast toast = Toast.makeText(getActivity(), "没有这个城市请重新输入", Toast.LENGTH_SHORT);
//屏幕下方显示,X轴和Y轴偏移量都是0
toast.setGravity(Gravity.BOTTOM, 0, 0);
toast.show();
Log.i("获取到数据:", responseString);
}
}
@Override
public void onComplete() {
Log.i("sdkdemo", "onComplete");
}
@Override
public void onError(int status, String responseString, Exception e) {
Log.i("sdkdemo", "onError, status: " + status);
Log.i("sdkdemo", "errMsg: " + (e == null ? "" : e.getMessage()));
}
});
}
}
}