Android Postcodes.io HTTP 请求
Android Postcodes.io HTTP Request
我正在使用 AsyncHttpClient class,但我无法将结果保存为 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" 存储到一个字符串中,但我什至无法将整个输出分配给一个字符串,因为我得到了 " 从内部 class 中访问变量 '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()
创建了匿名内部 class 的新实例。您要从内部访问的所有内容都应声明为父 class 的字段或包含方法中的最终变量。另一方面,您不能将 String 设为 final,因为 final 变量只会被赋值一次。您可以使用单个 String 参数创建包装器 class 或使 's' 成为父字段 class 或使用 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());
}
});
}
}
我正在使用 AsyncHttpClient class,但我无法将结果保存为 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" 存储到一个字符串中,但我什至无法将整个输出分配给一个字符串,因为我得到了 " 从内部 class 中访问变量 '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()
创建了匿名内部 class 的新实例。您要从内部访问的所有内容都应声明为父 class 的字段或包含方法中的最终变量。另一方面,您不能将 String 设为 final,因为 final 变量只会被赋值一次。您可以使用单个 String 参数创建包装器 class 或使 's' 成为父字段 class 或使用 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());
}
});
}
}