我有一个调用php页面从MySQL检索数据的方法。该方法在我的MainActivity类中运行得很好,但如果我将代码移动到另一个类中,则完全相同的代码不起作用。这是我的方法:
public void setRoundNumber() {
try{
httpclient = new DefaultHttpClient();
httppost = new HttpPost("http://brad.grublist.net/project/getRoundNumber.php");
nameValuePairsRoundNumber = new ArrayList<NameValuePair>(1);
nameValuePairsRoundNumber.add(new BasicNameValuePair("eventDate", GlobalClass.eventDate));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairsRoundNumber));
//Execute HTTP Post Request
ResponseHandler<String> responseHandler = new BasicResponseHandler();
final String json = httpclient.execute(httppost, responseHandler);
Log.e("Response: ", "> " + json);
if (json != null) {
try {
JSONObject jsonObj = new JSONObject(json);
if (jsonObj != null) {
JSONArray roundNumber = jsonObj.getJSONArray("roundNumber");
JSONObject catObj = (JSONObject) roundNumber.get(0);
String roundNum = catObj.getString("roundNumber");
GlobalClass.roundNumber = roundNum;
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("JSON Data", "Didn't receive any data from server!");
}
}catch(Exception e){
System.out.println("Exception : " + e.getMessage());
}
}
我的其他课程中是否有一些东西要让这种方法在那里工作?我也试过在我的MainActivity中将它作为一个静态方法,并尝试在其他类中访问它并且也没有运气。