0
我有一個PHP文件返回JSON數組,這裏是我的代碼連接到該PHP並在jsonArray中獲取響應。 這是正確的嗎?使用Android PHP和JSON處理logcat時出錯,
JSON響應從PHP
[{ 「ID」: 「1」, 「名」: 「Swapnil」, 「年齡」: 「21」, 「地址」: 「Nerul」, 「移動」 : 「4475478983」}]
activity_main.xml中
RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Display All"
android:id="@+id/send"
android:layout_marginTop="139dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/display"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
MainActivity.java
package com.lnt.swapnil;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity {
private Button sendButton;
private TextView displayTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendButton = (Button)findViewById(R.id.send);
displayTextView = (TextView)findViewById(R.id.display);
}
public void onClick(View view)
{
PHPConnector obj = new PHPConnector();
JSONArray myArray = obj.getAll();
setDisplayTextView(myArray);
}
public void setDisplayTextView(JSONArray jsonArray)
{
String s = "";
for (int i=0;i<jsonArray.length();i++)
{
JSONObject jsonObject = null;
try {
jsonObject = jsonArray.getJSONObject(i);
s = s + "Id : "+jsonObject.getString("id")+"\n\n"
+ "Name : "+jsonObject.getString("name")+"\n\n"
+ "Age : "+jsonObject.getString("age")+"\n\n"
+ "Address : "+jsonObject.getString("address")+"\n\n"
+ "Mobile : "+jsonObject.getString("mobile")+"\n\n";
displayTextView.setText(s);
}
catch (JSONException exp)
{
}
}
}
}
******************************************************************************
package com.lnt.swapnil;
import android.util.Log;
import org.json.JSONArray;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* Created by SWAPNIL on 20-08-2015.
*/
public class PHPConnector {
private JSONArray jsonArray;
public JSONArray getAll()
{
try {
URL url = new URL("http://127.0.0.1/Android/test.php");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
InputStream is = urlConnection.getInputStream();
BufferedReader streamReader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
StringBuilder responseStrBuilder = new StringBuilder();
String inputStr;
while ((inputStr = streamReader.readLine()) != null)
responseStrBuilder.append(inputStr);
jsonArray = new JSONArray(responseStrBuilder.toString());
}
catch (Exception e)
{
e.getMessage();
Log.i("Exception : ",e.getMessage());
}
return jsonArray;
}
}
***************************************************************************
**GetMember.java**
package com.lnt.swapnil;
import android.os.AsyncTask;
import org.json.JSONArray;
/**
* Created by SWAPNIL on 20-08-2015.
*/
public class GetMembers extends AsyncTask<PHPConnector,Long,JSONArray> {
@Override
protected JSONArray doInBackground(PHPConnector... phpConnectors) {
return phpConnectors[0].getAll();
}
}
** ** manifest.xml中
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lnt.swapnil" >
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
**登錄貓**
Process: com.lnt.swapnil, PID: 2288
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4007)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4002)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: println needs a message
at android.util.Log.println_native(Native Method)
at android.util.Log.i(Log.java:160)
at com.lnt.swapnil.PHPConnector.getAll(PHPConnector.java:40)
at com.lnt.swapnil.MainActivity.onClick(MainActivity.java:30)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4002)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
08-21 23:00:10.317 2288-2295/com.lnt.swapnil W/art﹕ Suspending all threads took: 44.072ms
同樣的問題再次........... –
我已經更新日誌貓... –
u能告訴我,在這行你所得到的例外。在下面的鏈接上也編輯了我的回答 – Aakash