我正在尝试使用 JSON 从 Internet 中的 php 文件中获取数据。它适用于普通类,但在使用片段时不起作用。
public static class MagFragment extends Fragment {
public static final String ARG_MAGAZINE_NUMBER = "magazine_number";
public MagFragment() {
// Empty constructor required for fragment subclasses
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
if(i != 0){
rootView = inflater.inflate(R.layout.fragment_magazine, container, false);
TextView magText = (TextView) rootView.findViewById(R.id.textViewMag);
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://csddata.site11.com");
HttpResponse response = httpclient.execute(httppost);
HttpEntity httpEntity = response.getEntity();
InputStream inputstream = httpEntity.getContent();
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(inputstream,"iso-8859-1"),8);
magText.setText(reader.readLine());
}catch (Exception e){
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return rootView;
}
}
它在一个普通的类中工作,所以它一定是因为它是一个片段。
我不知道如何复制和粘贴错误……但唯一说明错误在哪一行的错误是“HttpResponse response = httpclient.execute(httppost);” 线!