我没有任何语法错误。但是当我构建程序时,它显示编译错误如下:
error: incompatible types: int cannot be converted to String
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
这是我的json数组:
{"students":[
{"id":"1","firstname":"JOHN","lastname":"MATHEW","age":"25"},
{"id":"2","firstname":"SANDY","lastname":"SINGH","age":"23"},
{"id":"3","firstname":"MAYA","lastname":"TOMMY","age":"20"},
{"id":"4","firstname":"AMBI","lastname":"SHIZ","age":"25"}
]
}
这是代码。我正在使用Android Studio和Volley库。
MainActivity.java
public class MainActivity extends AppCompatActivity {
EditText firstname, lastname, age;
Button insert, show;
TextView result;
RequestQueue requestQueue;
String insertUrl = "http://localhost/android_volleytest/insertStudent.php";
String showUrl = "http://localhost/android_volleytest/connection.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstname = (EditText) findViewById(R.id.fname);
lastname = (EditText) findViewById(R.id.lname);
age = (EditText) findViewById(R.id.age);
insert = (Button) findViewById(R.id.insert_btn);
show = (Button) findViewById(R.id.show_btn);
result = (TextView) findViewById(R.id.txtView);
requestQueue = Volley.newRequestQueue(getApplicationContext());
show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
showUrl, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray students = response.getJSONArray("students");
for (int i = 0; i < students.length(); i++) {
JSONObject student = students.getJSONObject(i);
String firstname = student.getString("firstname");
String lastname = student.getString("lastname");
String age = student.getString("age");
result.append(firstname + " " + lastname + " " + age + "\n");
}
result.append("======\n");
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(jsonObjectRequest);
}
});
insert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
StringRequest request = new StringRequest(Request.Method.POST, insertUrl,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> parameters = new Hashtable<String, String>();
parameters.put("firstname", firstname.getText().toString());
parameters.put("lastname", lastname.getText().toString());
parameters.put("age", age.getText().toString());
return parameters;
}
};
requestQueue.add(request);
}
});
}
}
activity_main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
android:weightSum="1">
<EditText
android:id="@+id/fname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:hint="@string/edit_fname" />
<EditText
android:id="@+id/lname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:hint="@string/edit_lname" />
<EditText
android:id="@+id/age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:hint="@string/edit_age" />
<Button
android:id="@+id/insert_btn"
android:text="@string/btn_insert"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textSize="25sp"
android:onClick="startRequest"
/>
<TextView
android:id="@+id/txtView"
android:background="#99EB99"
android:layout_width="match_parent"
android:layout_height="285dp"
android:textSize="15sp" />
<Button
android:id="@+id/show_btn"
android:text="@string/btn_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textSize="25sp"
android:onClick="startRequest"
/>
</LinearLayout>
gradle console
Executing tasks: [clean, :app:compileDebugSources, :app:compileDebugAndroidTestSources]
Configuration on demand is an incubating feature.
:app:clean
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:preReleaseBuild UP-TO-DATE
:app:prepareComAndroidSupportAppcompatV72300Library
:app:prepareComAndroidSupportSupportV42300Library
:app:prepareDebugDependencies
:app:compileDebugAidl
:app:compileDebugRenderscript
:app:generateDebugBuildConfig
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources
:app:mergeDebugResources
:app:processDebugManifest
:app:processDebugResources
:app:generateDebugSources
:app:processDebugJavaRes UP-TO-DATE
:app:compileDebugJavaWithJavac
C:\Users\shabeer\AndroidStudioProjects\MyApplication\app\src\main\java\com\example\shabeer\myapplication\MainActivity.java:52: error: incompatible types: int cannot be converted to String
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
^
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error
FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 5.072 secs
答案 0 :(得分:0)
如果没有该POST请求的正文,请使用
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
showUrl, null, new Response.Listener<JSONObject>()...
而不是
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
showUrl, new Response.Listener<JSONObject>()...
因为您可以在 ..\src\main\java\com\android\volley\toolbox\JsonObjectRequest.java
中找到正确的语法,如下所示:
public JsonObjectRequest(int method, String url, JSONObject jsonRequest,
Listener<JSONObject> listener, ErrorListener errorListener)
和
public JsonObjectRequest(String url, JSONObject jsonRequest, Listener<JSONObject> listener,
ErrorListener errorListener)