我是Android开发的新手。我使用volly库连接到localhost。代码构建没有错误,但运行模拟器应用程序正在重述。
这是我的源代码。
signin.php
<?php
include_once("Dbclass.php");
$obj = new Dbconnection();
$id = $_GET['id'];
$sql = "select * from userdetails where UserId = '".$id."'";
$finalarr = array();
$result = mysqli_query($obj->getdbconnect(),$sql);
while($row = mysqli_fetch_array($result))
{
$name = $row['UserName'];
array_push($finalarr,array(
"name"=>$row['UserId'],
"username"=>$row['UserName']
)
);
}
echo json_encode(array("result"=>$finalarr));
?>
这是我的activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="29dp"
android:layout_marginRight="29dp"
android:layout_marginTop="61dp"
android:text="Button" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignTop="@+id/button"
android:layout_marginLeft="13dp"
android:layout_marginStart="13dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Name" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="350dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/button"
android:layout_marginTop="111dp"
/>
</RelativeLayout>
这是Config.java
package com.example.namdy.webservice;
/**
* Created by Namdy on 6/29/2017.
*/
public class Config {
public static final String DATA_URL = "http://10.0.2.2:8080/Signin.php?id=";
public static final String KEY_NAME = "username";
// public static final String KEY_ADDRESS = "address";
// public static final String KEY_VC = "vc";
public static final String JSON_ARRAY = "finalarray";
}
最后是MainActivity.java
package com.example.namdy.webservice;
import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity {
private EditText id;
private Button ok;
private TextView tx;
private ProgressDialog loading;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
id = (EditText) findViewById(R.id.editText);
ok = (Button) findViewById(R.id.button);
tx = (TextView) findViewById(R.id.textView);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String id = ok.getText().toString();
getdata(id);
}
});
}
private void getdata(String id)
{
loading = ProgressDialog.show(this,"Please wait...","Fetching...",false,false);
String url = Config.DATA_URL+id;
StringRequest srt = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
loading.dismiss();
showJSON(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this,error.getMessage().toString(),Toast.LENGTH_LONG).show();
}
}
);
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(srt);
}
private void showJSON(String response)
{
String usname = "";
try{
JSONObject obj = new JSONObject(response);
JSONArray result = obj.getJSONArray(Config.JSON_ARRAY);
JSONObject collegeData = result.getJSONObject(1);
usname = collegeData.getString(Config.KEY_NAME);
}
catch (JSONException e)
{
e.printStackTrace();
}
Toast.makeText(MainActivity.this,usname,Toast.LENGTH_LONG).show();
}
}
编辑: 这是我的logcat错误。
--------- beginning of crash
06-30 00:13:09.551 17261-17261/com.example.namdy.webservice E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.namdy.webservice, PID: 17261
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' on a null object reference
at com.example.namdy.webservice.MainActivity$3.onErrorResponse(MainActivity.java:65)
at com.android.volley.Request.deliverError(Request.java:524)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:101)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
06-30 00:13:21.778 17261-17292/com.example.namdy.webservice E/EGL_emulation: tid 17292: swapBuffers(487): error 0x300d (EGL_BAD_SURFACE)
06-30 00:13:21.778 17261-17292/com.example.namdy.webservice W/OpenGLRenderer: swapBuffers encountered EGL error 12301 on 0xa2256f80, halting rendering...
我的代码有什么问题?
谢谢!