我看過其他線程,但這並不能解決我的問題。 我複製了我以前的登錄類中的代碼,但突然出現此錯誤(登錄類正常工作)。org.json.JSONException:Value不能將type.lang.String轉換爲JSONObject
因此,這是一個具有類錯誤:
public class Absensi extends Activity {
private static final String TAG_SUCCESS = "success";
private ProgressDialog pDialog;
byte statusByte;
private String payload = "";
JSONParser jsonParser;
SessionManagement session;
TextView t= null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_absensi);
session = new SessionManagement(getApplicationContext());
jsonParser = new JSONParser();
}
public void onResume(){
super.onResume();
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
processIntent(getIntent());
new AbsenMahasiswa().execute();
}
}
public void onBackPressed() {
session.unsetDataKuliah();
super.onBackPressed();
}
void processIntent(Intent intent) {
NdefMessage[] message = getNdefMessages(getIntent());
for(int i=0; i<message.length; i++) {
for(int j=0; j<message[0].getRecords().length; j++){
NdefRecord record = message[i].getRecords()[j];
statusByte = record.getPayload()[0];
int languageCodeLength = statusByte & 0x3F;
int isUTF8 = statusByte-languageCodeLength;
if(isUTF8 == 0x00){
payload = new String(
record.getPayload(),1+languageCodeLength,
record.getPayload().length-1-languageCodeLength,
Charset.forName("UTF-8"));
}
else if (isUTF8 == -0x80){
payload = new String(
record.getPayload(),1+languageCodeLength,
record.getPayload().length-1-languageCodeLength,
Charset.forName("UTF-16"));
}
}
}
}
NdefMessage[] getNdefMessages(Intent intent){
NdefMessage[] msgs = null;
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())){
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if (rawMsgs != null) {
msgs = new NdefMessage[rawMsgs.length];
for (int i=0; i < rawMsgs.length; i++) {
msgs[i] = (NdefMessage) rawMsgs[i];
}
}
else {
byte[] empty = new byte[] {};
NdefRecord record = new NdefRecord(NdefRecord.TNF_UNKNOWN,
empty, empty, empty);
NdefMessage msg = new NdefMessage(new NdefRecord[] { record });
msgs = new NdefMessage[] { msg };
}
}
else {
Log.d("Peer to Peer 2", "Unknown intent.");
finish();
}
return msgs;
}
class AbsenMahasiswa extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
String request = "absen";
String npm = payload.substring(0,7);
String id_perangkat = payload.substring(8, payload.length());
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("request", request));
params.add(new BasicNameValuePair("npm", npm));
params.add(new BasicNameValuePair("id_perangkat", id_perangkat));
params.add(new BasicNameValuePair("kode", session.getKodeKuliah()));
params.add(new BasicNameValuePair("jenis", session.getJenisKuliah()));
params.add(new BasicNameValuePair("shift", session.getShiftKuliah()));
Log.d("DATA", jsonParser.makeHttpRequest(session.getUrl(),"POST", params).toString());
return null;
}
}
這是我的logcat和JSON是回頭率以前的對象:
09-22 14:54:39.847: E/JSON Parser(16997): Error parsing data org.json.JSONException: Value Cannot of type java.lang.String cannot be converted to JSONObject
09-22 14:54:39.847: D/DATA(16997): {"data":[{"jenis":"Praktikum","kode":"AIF102","jam":"12:00:00","nama":"Algoritma Struktur Dasar","shift":"2","hari":"Rabu","ruangan":"Lab917"}],"success":1}
我試着將它PHP並給了迴應:
{"success":1,"message":"Data proccessing success"}
JSON類別:
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// function get json from url
// by making HTTP POST or GET mehtod
public JSONObject makeHttpRequest(String url, String method, List<NameValuePair> params) {
// Making HTTP request
try {
// check for request method
if(method == "POST") {
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
else if(method == "GET") {
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
catch (ClientProtocolException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
}
catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
}
catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
數據中的字符串是一個有效的JSON之前,你應該初始化它作爲JSON對象的字符串。你在哪裏試圖轉換它? – Blackbelt 2014-09-22 08:14:00
您確定,您正在調用正確的API?這兩個JSON響應是不同的。 – 2014-09-22 08:14:55
你可以發佈JSONParser類嗎? – 2014-09-22 08:21:33