择忆霖心
2017-03-27 10:33
采纳率: 96.7%
浏览 856

求大神看看代码,帮帮我

public void onClick(View view) { new Thread(new Runnable() { @Override public void run() { try { String username = usernameEdit.getText().toString(); String password = passwordEdit.getText().toString(); OkHttpClient client = new OkHttpClient(); RequestBody requestBody = new FormBody.Builder() .add("username", username) .add("password", password) .build(); Request request = new Request.Builder() .url("http://test.ourwechat.com/test/auth.php") .post(requestBody) .build(); Response response = client.newCall(request).execute(); String responseData = response.body().string(); JSONObject object = new JSONObject(responseData); code = object.getInt("code"); if (code ==-1){ Toast.makeText(Login.this,"登录失败",Toast.LENGTH_SHORT).show(); }else if (code == 1){ Intent home = new Intent(Login.this, Homepage.class); startActivity(home); } } catch (JSONException e) { e.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } } }).start();
以上是代码
以下是错误日志
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.(Handler.java:200)
at android.os.Handler.(Handler.java:114)
at android.widget.Toast$TN.(Toast.java:348)
at android.widget.Toast.(Toast.java:101)
at android.widget.Toast.makeText(Toast.java:260)
at com.money.Login.Login$1$1.run(Login.java:90)
at java.lang.Thread.run(Thread.java:761)

求大神指出问题

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • stone_you 2017-03-28 01:56
    已采纳

    不要在子线程中使用toast

    点赞 评论
  • LittleMonsterr 2017-03-27 10:43
    点赞 评论
  • oyljerry 2017-03-27 13:24

    你所有UI操作要放到UI线程。
    http://www.2cto.com/kf/201405/304450.html

    点赞 评论

相关推荐 更多相似问题