我是一个非常新的android并且已经在android编程中跟踪了各种教程。我想要做的是登录https://ubresources.com。下面是我的java类
Results.java
package com.example.fetapp;
import java.io.IOException;
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.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class Results extends Activity {
private EditText email, pass;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
email = (EditText)findViewById(R.id.email);
pass = (EditText)findViewById(R.id.pass);
Button login = (Button)findViewById(R.id.login);
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
postLoginData();
startActivity(new Intent(Results.this, MainPage.class));
}
});
}
public void postLoginData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("https://ubresources.com/login.json");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", email.getText().toString);
nameValuePairs.add(new BasicNameValuePair("password", pass.getText().toString);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
Log.d("Login", response.toString());
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
} }
但仍然无法获得响应。请帮帮我,我的logcat信息看起来是
09-11 17:35:54.244: I/Choreographer(1197): Skipped 73 frames! The application may be doing too much work on its main thread.
09-11 17:35:54.284: D/gralloc_goldfish(1197): Emulator without GPU emulation detected.
09-11 17:35:55.916: I/Choreographer(1197): Skipped 90 frames! The application may be doing too much work on its main thread.
09-11 17:35:56.014: I/Choreographer(1197): Skipped 56 frames! The application may be doing too much work on its main thread.
09-11 17:35:56.114: I/Choreographer(1197): Skipped 46 frames! The application may be doing too much work on its main thread.
09-11 17:35:56.224: I/Choreographer(1197): Skipped 53 frames! The application may be doing too much work on its main thread.
09-11 17:35:56.324: I/Choreographer(1197): Skipped 46 frames! The application may be doing too much work on its main thread.
09-11 17:35:56.434: I/Choreographer(1197): Skipped 42 frames! The application may be doing too much work on its main thread.
09-11 17:35:56.553: I/Choreographer(1197): Skipped 71 frames! The application may be doing too much work on its main thread.
09-11 17:35:56.653: I/Choreographer(1197): Skipped 65 frames! The application may be doing too much work on its main thread.
09-11 17:35:56.763: I/Choreographer(1197): Skipped 52 frames! The application may be doing too much work on its main thread.
09-11 17:35:56.853: I/Choreographer(1197): Skipped 41 frames! The application may be doing too much work on its main thread.
09-11 17:35:57.204: I/Choreographer(1197): Skipped 39 frames! The application may be doing too much work on its main thread.
09-11 17:35:57.474: D/dalvikvm(1197): GC_FOR_ALLOC freed 217K, 5% free 6298K/6599K, paused 63ms, total 66ms
09-11 17:35:57.593: D/dalvikvm(1197): GC_FOR_ALLOC freed 198K, 6% free 6384K/6727K, paused 68ms, total 69ms
09-11 17:35:57.623: I/dalvikvm-heap(1197): Grow heap (frag case) to 7.392MB for 1165616-byte allocation
09-11 17:35:57.773: D/dalvikvm(1197): GC_CONCURRENT freed <1K, 5% free 7522K/7879K, paused 22ms+29ms, total 153ms
09-11 17:35:57.944: D/dalvikvm(1197): GC_FOR_ALLOC freed 0K, 5% free 7523K/7879K, paused 60ms, total 60ms
09-11 17:35:57.953: I/dalvikvm-heap(1197): Grow heap (frag case) to 7.887MB for 519172-byte allocation
09-11 17:35:58.074: D/dalvikvm(1197): GC_CONCURRENT freed 0K, 5% free 8030K/8391K, paused 20ms+9ms, total 120ms
09-11 17:35:58.074: D/dalvikvm(1197): WAIT_FOR_CONCURRENT_GC blocked 32ms
09-11 17:35:58.454: I/Choreographer(1197): Skipped 118 frames! The application may be doing too much work on its main thread.
09-11 17:35:59.164: I/Choreographer(1197): Skipped 193 frames! The application may be doing too much work on its main thread.
09-11 17:36:08.683: I/Choreographer(1197): Skipped 98 frames! The application may be doing too much work on its main thread.
09-11 17:36:08.803: I/Choreographer(1197): Skipped 63 frames! The application may be doing too much work on its main thread.
09-11 17:36:08.953: I/Choreographer(1197): Skipped 74 frames! The application may be doing too much work on its main thread.
09-11 17:36:09.094: I/Choreographer(1197): Skipped 87 frames! The application may be doing too much work on its main thread.
09-11 17:36:09.204: I/Choreographer(1197): Skipped 67 frames! The application may be doing too much work on its main thread.
09-11 17:36:09.324: I/Choreographer(1197): Skipped 56 frames! The application may be doing too much work on its main thread.
09-11 17:36:09.883: I/Choreographer(1197): Skipped 52 frames! The application may be doing too much work on its main thread.
09-11 17:36:10.004: I/Choreographer(1197): Skipped 56 frames! The application may be doing too much work on its main thread.
09-11 17:36:10.434: I/Choreographer(1197): Skipped 35 frames! The application may be doing too much work on its main thread.
09-11 17:36:10.833: I/Choreographer(1197): Skipped 87 frames! The application may be doing too much work on its main thread.
09-11 17:36:11.573: I/Choreographer(1197): Skipped 154 frames! The application may be doing too much work on its main thread.
09-11 17:36:16.523: I/Choreographer(1197): Skipped 78 frames! The application may be doing too much work on its main thread.
09-11 17:36:18.643: D/InputEventConsistencyVerifier(1197): KeyEvent: ACTION_UP but key was not down.
09-11 17:36:18.643: D/InputEventConsistencyVerifier(1197): in android.widget.EditText@4104d088
09-11 17:36:18.643: D/InputEventConsistencyVerifier(1197): 0: sent at 1890932000000, KeyEvent { action=ACTION_UP, keyCode=KEYCODE_TAB, scanCode=15, metaState=0, flags=0x8, repeatCount=0, eventTime=1890932, downTime=1890824, deviceId=0, source=0x101 }
09-11 17:36:30.733: I/Choreographer(1197): Skipped 36 frames! The application may be doing too much work on its main thread.
09-11 17:36:33.234: I/Choreographer(1197): Skipped 47 frames! The application may be doing too much work on its main thread.
09-11 17:36:34.583: D/AndroidRuntime(1197): Shutting down VM
09-11 17:36:34.583: W/dalvikvm(1197): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
09-11 17:36:34.653: E/AndroidRuntime(1197): FATAL EXCEPTION: main
09-11 17:36:34.653: E/AndroidRuntime(1197): android.os.NetworkOnMainThreadException
09-11 17:36:34.653: E/AndroidRuntime(1197): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
09-11 17:36:34.653: E/AndroidRuntime(1197): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
09-11 17:36:34.653: E/AndroidRuntime(1197): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
09-11 17:36:34.653: E/AndroidRuntime(1197): at java.net.InetAddress.getAllByName(InetAddress.java:214)
09-11 17:36:34.653: E/AndroidRuntime(1197): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
09-11 17:36:34.653: E/AndroidRuntime(1197): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
09-11 17:36:34.653: E/AndroidRuntime(1197): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
09-11 17:36:34.653: E/AndroidRuntime(1197): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
09-11 17:36:34.653: E/AndroidRuntime(1197): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
09-11 17:36:34.653: E/AndroidRuntime(1197): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
09-11 17:36:34.653: E/AndroidRuntime(1197): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
09-11 17:36:34.653: E/AndroidRuntime(1197): at com.example.fetapp.Results.postLoginData(Results.java:61)
09-11 17:36:34.653: E/AndroidRuntime(1197): at com.example.fetapp.Results$1.onClick(Results.java:42)
09-11 17:36:34.653: E/AndroidRuntime(1197): at android.view.View.performClick(View.java:4084)
09-11 17:36:34.653: E/AndroidRuntime(1197): at android.view.View$PerformClick.run(View.java:16966)
09-11 17:36:34.653: E/AndroidRuntime(1197): at android.os.Handler.handleCallback(Handler.java:615)
09-11 17:36:34.653: E/AndroidRuntime(1197): at android.os.Handler.dispatchMessage(Handler.java:92)
09-11 17:36:34.653: E/AndroidRuntime(1197): at android.os.Looper.loop(Looper.java:137)
09-11 17:36:34.653: E/AndroidRuntime(1197): at android.app.ActivityThread.main(ActivityThread.java:4745)
09-11 17:36:34.653: E/AndroidRuntime(1197): at java.lang.reflect.Method.invokeNative(Native Method)
09-11 17:36:34.653: E/AndroidRuntime(1197): at java.lang.reflect.Method.invoke(Method.java:511)
09-11 17:36:34.653: E/AndroidRuntime(1197): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-11 17:36:34.653: E/AndroidRuntime(1197): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-11 17:36:34.653: E/AndroidRuntime(1197): at dalvik.system.NativeStart.main(Native Method)
09-11 17:41:34.804: I/Process(1197): Sending signal. PID: 1197 SIG: 9
09-11 17:41:35.924: E/Trace(1213): error opening trace file: No such file or directory (2)
09-11 17:41:36.934: D/dalvikvm(1213): GC_FOR_ALLOC freed 164K, 5% free 6132K/6407K, paused 87ms, total 91ms
09-11 17:41:37.483: I/Choreographer(1213): Skipped 82 frames! The application may be doing too much work on its main thread.
09-11 17:41:37.523: D/gralloc_goldfish(1213): Emulator without GPU emulation detected.
答案 0 :(得分:1)
尝试使用Retrofit库。它还会为你解析json。您只需要为数据创建简单的java对象。
答案 1 :(得分:1)
您是否在自己的应用中加入了INTERNET权限?
您的错误似乎是因为您无法访问互联网,或者权限不太可能访问互联网。
尝试阅读Android有关连接的帮助,看看您是否忘记了任何内容:http://developer.android.com/training/basics/network-ops/connecting.html
并确保尝试这一点,也许你遇到了同样的问题:How to connect android emulator to the internet
答案 2 :(得分:1)
问题可以在您的logcat中直接看到。
<强> NetworkOnMainThreadException 强>
E/AndroidRuntime(1197): android.os.**NetworkOnMainThreadException** 09-11 17:36:34.653:
以下是对该文档的引用,基本上说您无法执行此操作。
http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html
DefaultHttpClient是一个相当低级别的类,因此比使用Retrofit更难以使用
http://square.github.io/retrofit
需要下载改造,并且需要进行一些设置,但从长远来看,使用更高级别的api进行Rest api调用会更快乐。
这是您可能感兴趣的另一个好资源。它描述了(或者至少指出了方向)将Squares的Retrofit和Otto(事件总线)组合成一种与Restful Web服务进行通信的非常好的方式
http://www.mdswanson.com/blog/2014/04/07/durable-android-rest-clients.html
答案 3 :(得分:0)
你的问题是你在检索响应时没有实现AsyncTask,或者你没有为互联网/网络添加权限?
尝试Android排球,它是推荐使用的,因为它由谷歌拥有,以及为您提供更快的网络访问。如果您尝试Android排球我可以通过多种方式帮助您通过我的电子邮件或推特到达我。干杯!