Hello我的代码生成一个简单的http请求并返回一个json。我的代码工作正常,但是当我尝试扩展AppCompatActivity时(因为我想在设备屏幕上显示json),它会给我带来多个错误,我的代码在下面
package com.vogella.java.library.okhttp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import java.io.IOException;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;
import org.json.JSONException;
import org.json.JSONObject;
public class TestMain extends AppCompatActivity {
OkHttpClient client = new OkHttpClient();
public static final MediaType JSON
= MediaType.parse("application/json; charset=utf-8");
String doPostRequest(String url, String json) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
Response response = client.newCall(request).execute();
return response.body().string();
}
public static void main(String[] args) throws IOException, JSONException {
String username = "test";
String password = "123";
TestMain example = new TestMain();
String json = "";
String postResponse = example.doPostRequest("http://86.145.89.53:8080/authentication_service-dev/authenticate?username=" + username + "&password=" + password, json);
int counter = postResponse.length();
if (counter == 14) {
System.out.println("Your username or password is invalid!!");
}
if (counter > 1000) {
System.out.println("HTTP Status 401 – Unauthhorized");
}
if (counter < 1000 && counter > 14) {
System.out.println("Welcome!!");
}
}}
错误是:
Exception in thread "main" java.lang.RuntimeException: Stub!
at android.content.Context.<init>(Context.java:20)
at android.content.ContextWrapper.<init>(ContextWrapper.java:21)
at android.view.ContextThemeWrapper.<init>(ContextThemeWrapper.java:21)
at android.app.Activity.<init>(Activity.java:22)
at android.support.v4.app.SupportActivity.<init>(SupportActivity.java:31)
at android.support.v4.app.BaseFragmentActivityGingerbread.<init>(BaseFragmentActivityGingerbread.java:37)
at android.support.v4.app.BaseFragmentActivityHoneycomb.<init>(BaseFragmentActivityHoneycomb.java:29)
at android.support.v4.app.BaseFragmentActivityJB.<init>(BaseFragmentActivityJB.java:30)
at android.support.v4.app.FragmentActivity.<init>(FragmentActivity.java:79)
at android.support.v7.app.AppCompatActivity.<init>(AppCompatActivity.java:61)
at com.vogella.java.library.okhttp.TestMain.<init>(TestMain.java:18)
at com.vogella.java.library.okhttp.TestMain.main(TestMain.java:52)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
答案 0 :(得分:0)
您不在Android dev中使用public static void main
。在活动中,您使用onCreate:
@Override
public void onCreate(Bundle sis){
super.onCreate(sis);
//Move the contents of "public static void main" here
}
移动代码后删除public static void main
。
答案 1 :(得分:0)
Android活动必须有onCreate()
方法,当您使用AppCompactAcitvity
扩展任何类时,必须实现某些方法
更改您的代码,删除主要方法,add onCreate()
并在那里执行操作。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_acremote);
}
查看活动生命周期Link。