我尝试从url接收json 但是运行应用程序时出现问题
错误:(34,83)错误:不兼容的类型:int无法转换为String 错误:任务':app:compileDebugJavaWithJavac'执行失败。
编译失败;有关详细信息,请参阅编译器错误输出。
package mystore.com.example.hossam_aristo.mysqldbok;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity {
TextView texv;
RequestQueue requestQueue;
String urlShowAllStudnet = "http://localhost/android/showAllStudent.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
texv = (TextView) findViewById(R.id.txtV);
requestQueue = Volley.newRequestQueue(this);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, urlShowAllStudnet,
new Response.Listener<JSONObject>(){
@Override
public void onResponse(JSONObject response) {
JSONArray jsonArray = response.getJSONArray("allstudent");
for(int i = 0; i < jsonArray.length() ; i++){
JSONObject res = jsonArray.getJSONObject(i);
String id = res.getString("id");
String name = res.getString("username");
String email = res.getString("email");
texv.append("\n " + id + name + email);
}
}
}, new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(jsonObjectRequest);
}
}
答案 0 :(得分:0)
更改您的代码
String id = res.getString("id");
到
String id = res.get("id").toString();
我认为这会奏效!