以前、jsonの解析に関して質問しました。それ以来、正しく機能するはずの例を見つけましたが、そうではありません。私が今持っているコードは次のとおりです:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView tv = (TextView)findViewById(R.id.result);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
String username = "exampleusername";
String password = "examplepassword";
String url = "http://www.example.com/api/core/v1/my/";
String unp = username+":"+password;
class MyUser {
public String firstName;
public String lastName;
}
try {
HttpClient client = new DefaultHttpClient();
HttpGet header = new HttpGet(url);
String encoded_login = Base64.encodeToString(unp.getBytes(), Base64.NO_WRAP);
header.setHeader(new BasicHeader("Authorization", "Basic "+encoded_login));
HttpResponse response = client.execute(header);
HttpEntity entity = response.getEntity();
BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()));
for (String line; (line = reader.readLine()) != null;) {
if (line.isEmpty()) break; // Stop when headers are completed. We're not interested in all the HTML.
final String newjson = line.replaceAll("throw 'allowIllegalResourceCall is false.';", "");
JSONObject mainObject = new JSONObject(newjson);
JSONObject uniObject = mainObject.getJSONObject("firstName");
// System.out.println(newjson + newjson.contains("firstName") + "," + newjson.contains("lastName"));
System.out.println(uniObject);
// System.out.println(uniURL);
}
このコードを使用すると、次のエラーが発生します。
W/System.err(910): org.json.JSONException: End of input at character 0 of
W/System.err(910): at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
W/System.err(910): at org.json.JSONTokener.nextValue(JSONTokener.java:97)
W/System.err(910): at org.json.JSONObject.<init>(JSONObject.java:154)
W/System.err(910): at org.json.JSONObject.<init>(JSONObject.java:171)
W/System.err(910): at basic.authentication.BasicAuthenticationActivity.onCreate(BasicAuthenticationActivity.java:84)
W/System.err(910): at android.app.Activity.performCreate(Activity.java:4465)
W/System.err(910): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
W/System.err(910): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
W/System.err(910): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
W/System.err(910): at android.app.ActivityThread.access$600(ActivityThread.java:123)
W/System.err(910): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
W/System.err(910): at android.os.Handler.dispatchMessage(Handler.java:99)
W/System.err(910): at android.os.Looper.loop(Looper.java:137)
W/System.err(910): at android.app.ActivityThread.main(ActivityThread.java:4424)
W/System.err(910): at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err(910): at java.lang.reflect.Method.invoke(Method.java:511)
W/System.err(910): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
W/System.err(910): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
W/System.err(910): at dalvik.system.NativeStart.main(Native Method)
私がプルダウンしているjson文字列の一部は次のとおりです:
{
"enabled" : true,
"email" : "example@gmail.com",
"firstName" : "example firstname",
"lastName" : "example lastname",
"name" : "example fullname",
"level" : {
私は今jsonととても混乱していて、なぜこれが機能しないのですか?