我写了一些代码来将数据发送到服务器以存储在数据库中。我还使用wamp来创建语言环境服务器
这是我的MainActivity.java:
package com.example.jawad.sze;
public class MainActivity extends ActionBarActivity {
EditText ed;
Button asd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed = (EditText)findViewById(R.id.editText);
asd=(Button)findViewById(R.id.button3);
asd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringRequest stringRequest = new StringRequest(Request.Method.POST, "http://192.168.1.105/sze/add_user.php", new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e("response",response);
Toast.makeText(mainActivity.this, "onResponse", LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(mainActivity.this, "onErrorResponse", LENGTH_SHORT).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> map = new HashMap<>();
map.put("name",ed.getText().toString());
Toast.makeText(mainActivity.this, "Map", LENGTH_SHORT).show();
return map;
}
};
Volley.newRequestQueue(getApplicationContext()).add(stringRequest);
}
}
我的朋友:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.example.jawad.sze"
minSdkVersion 18
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.mcxiaoke.volley:library:1.0.19'
}
记录错误:
05-26 22:56:28.354 4232-4495/com.example.jawad.sze E/Volley: [9108] NetworkDispatcher.run: Unhandled exception java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:208)
at android.os.Handler.<init>(Handler.java:122)
at android.widget.Toast$TN.<init>(Toast.java:327)
at android.widget.Toast.<init>(Toast.java:92)
at android.widget.Toast.makeText(Toast.java:241)
at com.example.jawad.sze.MainActivity$1$3.getParams(MainActivity.java:59)
at com.android.volley.Request.getBody(Request.java:434)
at com.android.volley.toolbox.HurlStack.addBodyIfExists(HurlStack.java:260)
at com.android.volley.toolbox.HurlStack.setConnectionParametersForRequest(HurlStack.java:234)
at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:107)
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:96)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
这段代码以前工作得很完美,我曾经把文字放在EditText中点击按钮,打开phpMyAdmin并且字符串会在那里,但不再是我更新了Android工作室但是我不认为&# 39;问题
答案 0 :(得分:0)
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:21.0.3'
'com.mcxiaoke.volley:library:1.0.19'
}
将我们的'com.mcxiaoke.volley:library:1.0.19'
更改为以下
compile 'com.mcxiaoke.volley:library:1.0.19'
答案 1 :(得分:0)
问题是Toast.makeText(mainActivity.this, "Map", LENGTH_SHORT).show();
StringRequest#getParams()
调用它的方法似乎是在Volley管理的线程上调用的。据推测,该线程尚未启动,因此RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
有关从异步服务使用UI的方法,请参阅https://developer.android.com/training/multiple-threads/communicate-ui.html。在这样的线程上创建祝酒词根本不起作用。