我是使用 HTTP 的新手,更多的是在 Android 中。我正在尝试/必须对使用 JSON 对象通过 HTTP Post 发送所需信息的登录名进行身份验证。
当我运行应用程序时,我使用登录屏幕登录,该屏幕要求将安装添加到访问 URL、用户名和密码中。我输入这些,点击确认按钮,然后应用程序崩溃并出现一条消息:“不幸的是,TesterField 已停止。”
我无法破译 LOGCAT 中识别的内容。
有人能告诉我这个(运行时)错误背后的原因吗?
代码:
package name.company.androidlogin;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class LoginScreen extends Activity {
private Button loginButton;
private TextView resultText;
private EditText usernameText;
private EditText passwordText;
private EditText installText;
private String accessURL = "http://url";
public final String CONSUMER_KEY = "KEY";
public final String CONSUMER_SECRET = "SECRET";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_screen);
loginButton = (Button) findViewById(R.id.btn_login);
resultText = (TextView) findViewById(R.id.lbl_result);
usernameText = (EditText) findViewById(R.id.txt_username);
passwordText = (EditText) findViewById(R.id.txt_password);
installText = (EditText) findViewById(R.id.txt_install);
loginButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.login_screen, menu);
return true;
}
public void sendJson()
{
JSONObject jOb;
try {
jOb = toJSON();
HttpPost httpPost = new HttpPost(accessURL);
StringEntity entity = new StringEntity(jOb.toString(), HTTP.UTF_8);
entity.setContentType("application/json");
httpPost.setEntity(entity);
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(httpPost);
resultText.setText(response.getStatusLine().getStatusCode()+" "+response.getStatusLine().getReasonPhrase());
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public JSONObject toJSON() throws JSONException {
JSONObject credentials = new JSONObject();
try {
credentials.put("ConsumerSecret", CONSUMER_SECRET);
credentials.put("ConsumerKey", CONSUMER_KEY);
credentials.put("Password", passwordText.getText());
credentials.put("Username", usernameText.getText());
} finally {
resultText.setText(credentials.toString(2));
}
return credentials;
}
}
日志猫:
07-16 13:55:50.834: I/ActivityManager(93): START {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=name.company.testerfield/.URLBuilder bnds=[120,402][240,552]} from pid 192
07-16 13:55:50.834: W/WindowManager(93): Failure taking screenshot for (180x300) to layer 21005
07-16 13:55:50.954: D/dalvikvm(1122): Not late-enabling CheckJNI (already on)
07-16 13:55:50.997: I/ActivityManager(93): Start proc name.company.testerfield for activity name.company.testerfield/.URLBuilder: pid=1122 uid=10040 gids={3003}
07-16 13:55:51.134: I/WindowManager(93): createSurface Window{415d4950 Starting name.company.testerfield paused=false}: DRAW NOW PENDING
07-16 13:55:51.474: W/NetworkManagementSocketTagger(93): setKernelCountSet(10040, 1) failed with errno -2
07-16 13:55:52.474: I/WindowManager(93): createSurface Window{41620b70 name.company.testerfield/name.company.testerfield.URLBuilder paused=false}: DRAW NOW PENDING
07-16 13:55:52.604: D/gralloc_goldfish(1122): Emulator without GPU emulation detected.
07-16 13:55:52.704: I/ActivityManager(93): Displayed name.company.testerfield/.URLBuilder: +1s788ms
07-16 13:55:52.974: W/NetworkManagementSocketTagger(93): setKernelCountSet(10005, 0) failed with errno -2
07-16 13:55:55.054: I/WindowManager(93): createSurface Window{415cdbe0 InputMethod paused=false}: DRAW NOW PENDING
07-16 13:55:56.084: D/dalvikvm(147): GC_CONCURRENT freed 306K, 35% free 10784K/16455K, paused 6ms+8ms
07-16 13:56:09.504: D/dalvikvm(93): GC_CONCURRENT freed 483K, 10% free 12354K/13639K, paused 6ms+33ms
07-16 13:56:13.364: D/dalvikvm(376): GC_CONCURRENT freed 499K, 6% free 11433K/12039K, paused 6ms+27ms
07-16 13:56:17.134: D/AndroidRuntime(1122): Shutting down VM
07-16 13:56:17.134: W/dalvikvm(1122): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
07-16 13:56:17.164: E/AndroidRuntime(1122): FATAL EXCEPTION: main
07-16 13:56:17.164: E/AndroidRuntime(1122): java.lang.NullPointerException
07-16 13:56:17.164: E/AndroidRuntime(1122): at java.net.URI.parseURI(URI.java:353)
07-16 13:56:17.164: E/AndroidRuntime(1122): at java.net.URI.<init>(URI.java:204)
07-16 13:56:17.164: E/AndroidRuntime(1122): at java.net.URI.create(URI.java:725)
07-16 13:56:17.164: E/AndroidRuntime(1122): at org.apache.http.client.methods.HttpPost.<init>(HttpPost.java:79)
07-16 13:56:17.164: E/AndroidRuntime(1122): at name.company.testerfield.URLBuilder.postLoginData(URLBuilder.java:57)
07-16 13:56:17.164: E/AndroidRuntime(1122): at name.company.testerfield.URLBuilder.onClick(URLBuilder.java:50)
07-16 13:56:17.164: E/AndroidRuntime(1122): at android.view.View.performClick(View.java:3480)
07-16 13:56:17.164: E/AndroidRuntime(1122): at android.view.View$PerformClick.run(View.java:13983)
07-16 13:56:17.164: E/AndroidRuntime(1122): at android.os.Handler.handleCallback(Handler.java:605)
07-16 13:56:17.164: E/AndroidRuntime(1122): at android.os.Handler.dispatchMessage(Handler.java:92)
07-16 13:56:17.164: E/AndroidRuntime(1122): at android.os.Looper.loop(Looper.java:137)
07-16 13:56:17.164: E/AndroidRuntime(1122): at android.app.ActivityThread.main(ActivityThread.java:4340)
07-16 13:56:17.164: E/AndroidRuntime(1122): at java.lang.reflect.Method.invokeNative(Native Method)
07-16 13:56:17.164: E/AndroidRuntime(1122): at java.lang.reflect.Method.invoke(Method.java:511)
07-16 13:56:17.164: E/AndroidRuntime(1122): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-16 13:56:17.164: E/AndroidRuntime(1122): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-16 13:56:17.164: E/AndroidRuntime(1122): at dalvik.system.NativeStart.main(Native Method)
07-16 13:56:17.204: W/ActivityManager(93): Force finishing activity name.company.testerfield/.URLBuilder
07-16 13:56:17.204: W/WindowManager(93): Failure taking screenshot for (180x300) to layer 21010
07-16 13:56:17.344: I/WindowManager(93): createSurface Window{41642a88 paused=false}: DRAW NOW PENDING
07-16 13:56:17.724: W/ActivityManager(93): Activity pause timeout for ActivityRecord{4133b488 name.company.testerfield/.URLBuilder}
07-16 13:56:17.743: W/NetworkManagementSocketTagger(93): setKernelCountSet(10005, 1) failed with errno -2
07-16 13:56:17.814: I/WindowManager(93): createSurface Window{41622c28 com.android.launcher/com.android.launcher2.Launcher paused=false}: DRAW NOW PENDING
07-16 13:56:18.694: W/NetworkManagementSocketTagger(93): setKernelCountSet(10040, 0) failed with errno -2
07-16 13:56:28.634: W/ActivityManager(93): Activity destroy timeout for ActivityRecord{4133b488 name.company.testerfield/.URLBuilder}
编辑:我应该提到一些其他要求: - 将内容类型设置为“application/json; charset=utf-8″ - 将接收 cookie 作为响应的一部分
谢谢