//Below is the entire class from which i'm trying to start an activity from
public class SimpleHttpClient extends Activity {
public static final int HTTP_TIMEOUT = 30 * 1000;
private static HttpClient mHttpClient;
private static HttpClient getHttpClient() {
if (mHttpClient == null) {
mHttpClient = new DefaultHttpClient();
final HttpParams params = mHttpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, HTTP_TIMEOUT);
HttpConnectionParams.setSoTimeout(params, HTTP_TIMEOUT);
ConnManagerParams.setTimeout(params, HTTP_TIMEOUT);
}
return mHttpClient;
}
public static String executeHttpPost(String url, ArrayList<NameValuePair> postParameters) throws Exception {
BufferedReader in = null;
try {
HttpClient client = getHttpClient();
HttpPost request = new HttpPost(url);
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
request.setEntity(formEntity);
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
String result = sb.toString();
String disabled = "Disabled";
if (result.length() == 2)
{
return "Disabled";
}
else if (result.length() == 3)
{
// return "Logged In";
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
//it tells me i cannot reference from a static context
}
else if (result.length() == 5)
{
return "Unknown";
}
else if (result.length() == 4)
{
return "Incorrect Login Details";
}
else {
return result+"Unknown"+result.length();
}
}
finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static String executeHttpGet(String url) throws Exception {
BufferedReader in = null;
try {
HttpClient client = getHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(url));
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
String result = sb.toString();
return result;
}
finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
答案 0 :(得分:0)
startActivity
是Activity类中的非静态方法,您尝试从静态方法调用它,这就是编译器不满意的原因。您可以参考api签名here
因此要么从getHttpClient,executeHttpPost方法和mHttpClient字段中删除static关键字,它应该向前移动。
或者您可以使用上下文来调用startActivity,如:
context.startActivity(i);
在创建意图时传递上下文而不是此引用。
答案 1 :(得分:0)
您应该将上下文传递给静态方法:
public static String executeHttpPost(Context context, String url, ArrayList<NameValuePair> postParameters) throws Exception {
...
// return "Logged In";
Intent i = new Intent(context, MainActivity.class);
context.startActivity(i);
...
}
答案 2 :(得分:0)
您需要提供Context
来启动Intent
。在你的情况下你应该改变:
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
为:
Context context;
// init context here or provide it as a static method argument
Intent i = new Intent(this, MainActivity.class);
context.startActivity(i);
因此该方法应如下所示:
public static String executeHttpPost(Context context, String url, ArrayList<NameValuePair> postParameters) throws Exception {
// ...
else if (result.length() == 3)
{
Intent i = new Intent(this, MainActivity.class);
context.startActivity(i);
}
// ...
}