我使用的是AsyncHttpClient类,无法将结果保存为OnSuccess函数中的字符串。下面是我想要解析的结果的示例,我正在尝试检索"lsoa"值。
{
"status" : 200,
"result" : {
"postcode" : "NW2 1SJ",
"quality" : 1,
"eastings" : 524332,
"northings" : 186558,
"country" : "England",
"nhs_ha" : "London",
"longitude" : -0.207661046004569,
"latitude" : 51.5642021415348,
"parliamentary_constituency" : "Finchley and Golders Green",
"european_electoral_region" : "London",
"primary_care_trust" : "Barnet",
"region" : "London",
"lsoa" : "Barnet 040A",
"msoa" : "Barnet 040",
"incode" : "1SJ",
"outcode" : "NW2",
"admin_district" : "Barnet",
"parish" : "Barnet, unparished area",
"admin_county" : null,
"admin_ward" : "Childs Hill",
"ccg" : "NHS Barnet",
"nuts" : "Barnet",
"codes" : {
"admin_district" : "E09000003",
"admin_county" : "E99999999",
"admin_ward" : "E05000045",
"parish" : "E43000193",
"ccg" : "E38000005",
"nuts" : "UKI71"
}
}
我想做的只是将API中的"lsoa"存储到一个字符串中,但我甚至无法将整个输出分配给一个字符串,因为我得到了一个"变量的s"从内部类中访问,需要声明为最终"错误。
private void queryPostcodes(String searchString){
String s;
// Create a client to perform API request
AsyncHttpClient client = new AsyncHttpClient();
mDialog.show();
// Output the link being checked
Log.d("Gentrification: ", QUERY_URL + searchString);
// Have the client get a JSONArray of data and define how to respond
client.get(QUERY_URL + searchString, new JsonHttpResponseHandler(){
@Override
public void onSuccess(JSONObject jsonObject){
mDialog.dismiss();
Log.d("X: ", jsonObject.toString());
s = jsonObject.toString(); //<-- DOESN'T WORK :(
}
@Override
public void onFailure(int statusCode, Throwable throwable, JSONObject error){
mDialog.dismiss();
Toast.makeText(getApplicationContext(), "Error: " + statusCode + " " + throwable.getMessage() + " - Postcode not found", Toast.LENGTH_LONG).show();
Log.e("X", statusCode + " " + throwable.getMessage());
}
});
}
如有任何帮助,我们将不胜感激。
如果你尝试在AndroidStudio中按Alt+Enter,你会得到一个快速修复选项:"将‘s‘转换为一个元素数组’。点击并瞧。
问题的原因是new JsonHttpResponseHandler()
创建了匿名内部类的新实例。要从内部访问的所有内容都应声明为父类的字段或包含方法中的最终变量。另一方面,您不能将String设置为final,因为final变量只分配一次。您可以使用单个String参数创建一个包装类,或者使"s"成为父类的字段,或者使用String[]作为包装。
字符串包装选项:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final StringBuffer stringBuffer = new StringBuffer();
Button updateButton = (Button) findViewById(R.id.update);
updateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
stringBuffer
.delete(0, stringBuffer.length())
.append(System.currentTimeMillis());
}
});
}
}
父选项字段:
public class MainActivity extends AppCompatActivity {
private String s;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button updateButton = (Button) findViewById(R.id.update);
updateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
s = String.valueOf(System.currentTimeMillis());
}
});
}
}
字符串[]选项:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String[] s = new String[1];
Button updateButton = (Button) findViewById(R.id.update);
updateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
s[0] = String.valueOf(System.currentTimeMillis());
}
});
}
}