Home > Mobile >  Why android end use yahoo weather API connection, but can't obtain any data?
Why android end use yahoo weather API connection, but can't obtain any data?

Time:09-21

Use the sample code provided by the website, anyway don't get any data? Troublesome everybody bosses to help me have a look, is there is something wrong with the code or now temporarily can't gain

ExampleRequest. Java
The import com. Android. Volley. AuthFailureError;
The import com.android.volley.Net workResponse;
The import com. Android. Volley. ParseError;
The import com. Android. Volley. The Response;
The import com. Android. Volley. Toolbox. HttpHeaderParser;
The import com. Android. Volley. Toolbox. JsonRequest;
The import com. Google. Gson. JsonSyntaxException;

The import net. Request. The request;
The import net. Request. OAuthAccessor;
The import net. Request. OAuthConsumer;
The import net. Request. OAuthException;
The import net. Request. OAuthMessage;

import java.io.IOException;
Import the Java. IO. UnsupportedEncodingException;
The import java.net.URISyntaxException;
Import the Java. Util. HashMap;
import java.util.Map;


Public class ExampleRequest Extends JsonRequest {

Final String appId="aGMrrF4c";
Final String CONSUMER_KEY="dj0yJmk9ZGM1eW1CQmFxelVDJmQ9WVdrOVlVZE5jbkpHTkdNbWNHbzlNQS0tJnM9Y29uc3VtZXJzZWNyZXQmc3Y9MCZ4PWFk";
Final String CONSUMER_SECRET="29 bb41285bb99214a0366f3b7b401dc941d9baf9";

Final String baseUrl="https://weather-ydn-yql.media.yahoo.com/forecastrss";


Public ExampleRequest (int method, url String, the String requestBody, Response. Listener The listener, the Response ErrorListener ErrorListener) {
Super (method, url, requestBody, listener, errorListener);
}

@ Override
Public Map GetHeaders () throws AuthFailureError {
Map Headers=new HashMap<> (a);
OAuthConsumer consumer=new OAuthConsumer (null, CONSUMER_KEY, CONSUMER_SECRET, null);
Consumer. SetProperty (request. OAUTH_SIGNATURE_METHOD, request. HMAC_SHA1);
OAuthAccessor accessor=new OAuthAccessor (consumer);


Try {
OAuthMessage request=accessor. NewRequestMessage (OAuthMessage. GET getUrl (), null);
String=authorization request. GetAuthorizationHeader (null);
Headers. The put (" Authorization ", Authorization);
} the catch (OAuthException e) {
e.printStackTrace();
} the catch (IOException e) {
e.printStackTrace();
} the catch (URISyntaxException e) {
e.printStackTrace();
}
Headers. The put (" X - Yahoo - App - Id ", appId);
The content-type headers. The put (" ", "application/json");
Return headers.
}

@ Override
Public String getUrl () {
Return baseUrl + "? Location=in sunnyvale, ca& Format=json ";
}

@ Override
Protected Response ParseNetworkResponse (NetworkResponse response) {
Try {
String json=new String (the response data, HttpHeaderParser. ParseCharset (. The response headers));
T parsedResponse=parseResponse (json);
Return the Response. Success (parsedResponse, HttpHeaderParser parseCacheHeaders (Response));
} the catch (UnsupportedEncodingException | JsonSyntaxException e) {
Return the Response. The error (new ParseError (e));
}
}

Protected T parseResponse (String jsonObject) {
return null;//Add the response parsing here
}


ExampleRequestManager. Java
import android.content.Context;

The import com. Android. Volley. Request;
The import com. Android. Volley. RequestQueue;
The import com. Android. Volley. Toolbox. Volley;

Public class ExampleRequestManager {
Private static ExampleRequestManager sInstance;

The Context mContext;
RequestQueue mRequestQueue;

Public static synchronized ExampleRequestManager getInstance (Context Context) {
If (sInstance==null) {
SInstance=new ExampleRequestManager (context);
}
Return sInstance;
}

Private ExampleRequestManager Context (Context) {
MContext=context;
MRequestQueue=Volley. NewRequestQueue (mContext);
}

The public & lt; T> Void addToRequestQueue (Request Request) {
MRequestQueue. Add (request);
}
}


MainActivity
The import androidx. Appcompat. App. AppCompatActivity;

import android.os.Bundle;
The import android. Widget. TextView;
import android.widget.Toast;

The import com. Android. Volley. AuthFailureError;
The import com. Android. Volley. Request;
The import com. Android. Volley. The Response;
The import com. Android. Volley. VolleyError;

Public class MainActivity extends AppCompatActivity {
Private TextView TV;

@ Override
Protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

TV=the findViewById (R.i which v);
}

@ Override
Public void onStart () {

Final ExampleRequestManager requestManager=ExampleRequestManager. GetInstance (this);
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related