Searching Twitter with OAuth - java
Okay so I get an access token for Twitter each time I run my program...(It's tedious at the moment but I will find out how to make it a persistent store)... How do I go about using this access token so that when I search, I won't get a
"Rate limit exceeded. Clients may not make more than 150 requests per hour."
error?
It happens when I'm searching for the following: "https://api.twitter.com/1/users/show.json?screen_name=[screenName]"
Which is coded as :
BufferedReader ff = new BufferedReader( new InputStreamReader(ffUser.openConnection().getInputStream()));
In my code below:
public class UDC {
private static String term1;
private static String term2;
public static String PIN;
private static final String twitterSearch = "http://search.twitter.com/search.json?q=%23";
private static String rppPage = "&rpp=500&page=1";
private static final String ffGet = "https://api.twitter.com/1/users/show.json?screen_name=";
private static final String CONSUMER_KEY = "K7el7Fqu7UtcJv3A3ssOQ";
private static final String CONSUMER_SECRET = "w7ZX27ys58mafLYeivuA2POVe0gjhTIIUH26f2IM";
private static String entities = "&include_entities=true";
static Object[][] tableData = new Object[500][6];
static SearchResultC s = new SearchResultC();
static StringBuffer buff = new StringBuffer();
static StringBuffer buff1 = new StringBuffer();
public static void main (String args[]) throws Exception{
verifyURL v = new verifyURL();
Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
RequestToken requestToken = twitter.getOAuthRequestToken();
AccessToken accessToken = null; // = loadAccessToken(Integer.parseInt(args[0]));
//Twitter twitter = factory.getInstance);
//twitter.setOAuthConsumerKey(COMSUMER_KEY, COMSUMER_SECRET);
//twitter.setOAuthAccessToken(accessToken);
v.valURLText.setText(requestToken.getAuthorizationURL());
v.vFrame.setVisible(true);
int p = 0;
do {
//nothing
} while (v.vFrame.isVisible());
try {
if (PIN.length() > 0) {
accessToken = twitter.getOAuthAccessToken(requestToken, PIN);
} else {
accessToken = twitter.getOAuthAccessToken();
}
} catch (TwitterException te) {
if(401 == te.getStatusCode()) {
showErrorPane("Unable to get access code", "Error");
p = 1;
} else {
te.printStackTrace();
}
}
//storeAccessToken(twitter.verifyCredentials().getId(), accessToken);
if (p == 0) {
initComponents();
UDTFrame.setVisible(true);
} else {
System.exit(0);
}
}
#SuppressWarnings({ "static-access"})
private static void searchButtonMouseClicked(String t1, String t2) throws IOException {
if(t1.equals("") || t2.equals("") || t1.equals(t2))
{
showErrorPane("Invalid Search Terms", "Search Error");
}
else
{
s.getInitComponents();
clicked(t1, 0);
clicked(t2, 3);
s.SRTFrame.setVisible(true);
s.sTerm1Field.setText(t1);
s.sTerm2Field.setText(t2);
}
}
#SuppressWarnings("static-access")
public static void clicked(String term, int t){
UDTFrame.setVisible(false);
float follower;
float friends;
float ffRatio;
float DUA;
int statuses;
int day;
int year;
String month;
try {
URL searchURL1 = new URL (twitterSearch + term + rppPage);
//String searchURL = new String (twitterSearch + term + rppPage);
BufferedReader br = new BufferedReader( new InputStreamReader(searchURL1.openConnection().getInputStream()));
//OAuthRequest request = new OAuthRequest(Verb.POST, searchURL);
int c;
while ((c=br.read()) != -1) {
buff.append((char)c);
}
br.close();
/*******************************************************************************************/
/*******************************************************************************************/
/******************************** follower/friend ratio ************************************/
/*******************************************************************************************/
/*******************************************************************************************/
JSONObject js = new JSONObject(buff.toString());
JSONArray tweets = js.getJSONArray("results");
JSONObject tweet = new JSONObject();
for(int i=0; i < tweets.length(); i++) {
tweet = tweets.getJSONObject(i);
//System.out.println(tweet);
//user[i] = tweet.getString("from_user_name");
//System.out.println(tweet.getString("from_user_name"));
//System.out.println(user[i]);
String userName = tweet.getString("from_user");
//URL ffUser = new URL(ffGet + user[i] + entities);
URL ffUser = new URL(ffGet + userName + entities);
String ffUser1 = new String(ffGet + userName + entities);
BufferedReader ff = new BufferedReader( new InputStreamReader(ffUser.openConnection().getInputStream()));
OAuthRequest request = new OAuthRequest(Verb.POST, ffUser1);
int d, e = 0;
while((d = ff.read()) != -1) {
buff1.append((char)d);
e++;
}
ff.close();
JSONObject js1 = new JSONObject(buff1.toString());
//System.out.println(js1);
//JSONArray userData = new JSONArray(buff1.toString());
//JSONObject userData1;
//for(int j = 0; j < js1.length(); i++){
//userData1 = userData.getJSONObject(j);
follower = js1.getInt("followers_count");
friends = js1.getInt("friends_count");
ffRatio = friends/follower;
String createdDate = js1.getString("created_at");
statuses = js1.getInt("statuses_count");
String nameData = js1.getString("name");
String gen = gender(nameData);
//}
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I'm completely new to this OAuth and Access Tokens and all so any help will be much appreciated.
With
OAuthRequest request = new OAuthRequest(Verb.POST, ffUser1);
you are doing an unauthenticated request using the Scribe library (you never instantiated an OAuthService object which would have to be used to sign this request). So when you do this too often Twitter denies these requests.
So your problem here does indeed come from mixing Twitter4J and Scribe.
Related
unable to refresh a jtable more than one time
In my jFrame,I created a table that contains the patients' list and a button that allows to refresh the table on action. When I click the refresh button for the firt time, the list is displayed in the table. Howerver, when I press it for a second time it doesn't work any more and the table isn't able to be refreshed. Any idea about the problem please? this is the code of the refresh button: public class Interface extends javax.swing.JFrame{ private void RefreshActionPerformed(java.awt.event.ActionEvent evt) { DefaultTableModel model = (DefaultTableModel)worklisttable.getModel(); int rowCount = model.getRowCount(); for (int i = rowCount - 1; i >= 0; i--) { model.removeRow(i); } model.fireTableDataChanged(); GetMwl_3.main(args); } public static void main(String args[]) { ...... } } GetMwl_3.java : allows to retrieve the patient list from the server public class GetMwl_3 { private String[] args; /** * #param args */ public static void main(String[] args/*,String aet, String host, Integer port*/) { new GetMwl_3(/*aet,host,port*/); } private static final int[] RETURN_KEYS = { Tag.AccessionNumber, Tag.ReferringPhysicianName, Tag.PatientName, Tag.PatientID, Tag.PatientBirthDate, Tag.PatientSex, Tag.PatientWeight, Tag.MedicalAlerts, Tag.Allergies, Tag.PregnancyStatus, Tag.StudyInstanceUID, Tag.RequestingPhysician, Tag.RequestingService, Tag.RequestedProcedureDescription, Tag.AdmissionID, Tag.SpecialNeeds, Tag.CurrentPatientLocation, Tag.PatientState, Tag.RequestedProcedureID, Tag.RequestedProcedurePriority, Tag.PatientTransportArrangements, Tag.PlacerOrderNumberImagingServiceRequest, Tag.FillerOrderNumberImagingServiceRequest, Tag.ConfidentialityConstraintOnPatientDataDescription, }; private static final int[] SPS_RETURN_KEYS = { Tag.Modality, Tag.RequestedContrastAgent, Tag.ScheduledStationAETitle, Tag.ScheduledProcedureStepStartDate, Tag.ScheduledProcedureStepStartTime, Tag.ScheduledPerformingPhysicianName, Tag.ScheduledProcedureStepDescription, Tag.ScheduledProcedureStepID, Tag.ScheduledStationName, Tag.ScheduledProcedureStepLocation, Tag.PreMedication, Tag.ScheduledProcedureStepStatus }; private static final String[] LE_TS = { UID.ExplicitVRLittleEndian, UID.ImplicitVRLittleEndian }; private static final byte[] EXT_NEG_INFO_FUZZY_MATCHING = { 1, 1, 1 }; private Device device; private final NetworkApplicationEntity remoteAE = new NetworkApplicationEntity(); private final NetworkConnection remoteConn = new NetworkConnection(); private final NetworkApplicationEntity ae = new NetworkApplicationEntity(); private final NetworkConnection conn = new NetworkConnection(); private final DicomObject keys = new BasicDicomObject(); private final DicomObject spsKeys = new BasicDicomObject(); private Association assoc; private int priority = 0; private int cancelAfter = Integer.MAX_VALUE;//ÐœÐ°ÐºÑ ÐºÐ¾Ð»Ð¸Ñ‡ÐµÑтво Ñтрок private boolean fuzzySemanticPersonNameMatching; private Component emptyLabel; public GetMwl_3(/*String aet, String host, Integer port*/) { String name = "DCMMWL"; device = new Device(name); NewThreadExecutor executor = new NewThreadExecutor(name); remoteAE.setInstalled(true); remoteAE.setAssociationAcceptor(true); remoteAE.setNetworkConnection(new NetworkConnection[] { remoteConn }); device.setNetworkApplicationEntity(ae); device.setNetworkConnection(conn); ae.setNetworkConnection(conn); ae.setAssociationInitiator(true); ae.setAETitle(name); for (int i = 0; i < RETURN_KEYS.length; i++) { keys.putNull(RETURN_KEYS[i], null); } keys.putNestedDicomObject(Tag.RequestedProcedureCodeSequence, new BasicDicomObject()); keys.putNestedDicomObject(Tag.ScheduledProcedureStepSequence, spsKeys); for (int i = 0; i < SPS_RETURN_KEYS.length; i++) { spsKeys.putNull(SPS_RETURN_KEYS[i], null); } spsKeys.putNestedDicomObject(Tag.ScheduledProtocolCodeSequence, new BasicDicomObject()); ///////// Properties pacs = new Properties(); InputStream file = null; File thefile = new File("C:\\properties\\save.properties"); if (thefile.exists()){ try { file = new FileInputStream("C:\\properties\\save.properties"); // load a properties file pacs.load(file); // get the property value and print it out remoteAE.setAETitle( pacs.getProperty("aethl7")); remoteConn.setHostname(pacs.getProperty("hosthl7")); remoteConn.setPort(Integer.parseInt(pacs.getProperty("porthl7"))); } catch (IOException ex) { ex.printStackTrace(); } finally { if (file != null) { try { file.close(); } catch (IOException e) { e.printStackTrace(); } } } addSpsMatchingKey(Tag.ScheduledProcedureStepStartDate,"20131030"); setTransferSyntax(LE_TS); long t1 = System.currentTimeMillis(); try { assoc = ae.connect(remoteAE, executor); } catch (Exception e) { // System.err.println("ERROR: Failed to establish association:"); //e.printStackTrace(System.err); //jLabel7.setText("ERROR: Failed to establish association"); javax.swing.JOptionPane.showMessageDialog(null,"Liste des patients ne peut pas être retirée"); //System.exit(2); } long t2 = System.currentTimeMillis(); System.out.println("Connected to " + remoteAE + " in " + ((t2 - t1) / 1000F) + "s"); //jLabel6.setText("Connected to " + remoteAE + " in " // + ((t2 - t1) / 1000F) + "s"); try { List<DicomObject> result = query(); long t3 = System.currentTimeMillis(); System.out.println("Received " + result.size() + " matching entries in " + ((t3 - t2) / 1000F) + "s"); for(DicomObject dcm : result) { worklist.main(args, dcm.getString(Tag.PatientID), dcm.getString(Tag.AccessionNumber), dcm.getString(Tag.PatientName), dcm.getString(Tag.PatientBirthDate), dcm.getString(Tag.PatientSex), dcm.getString(Tag.ReferringPhysicianName) ); } javax.swing.JOptionPane.showMessageDialog(null,"Liste des patients retirée avec succée"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { assoc.release(true); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("Released connection to " + remoteAE); } } public void setTransferSyntax(String[] ts) { TransferCapability tc = new TransferCapability( UID.ModalityWorklistInformationModelFIND, ts, TransferCapability.SCU); if (fuzzySemanticPersonNameMatching) tc.setExtInfo(EXT_NEG_INFO_FUZZY_MATCHING); ae.setTransferCapability(new TransferCapability[]{tc}); } public void addSpsMatchingKey(int tag, String value) { spsKeys.putString(tag, null, value); } public List<DicomObject> query() throws IOException, InterruptedException { TransferCapability tc = assoc.getTransferCapabilityAsSCU( UID.ModalityWorklistInformationModelFIND); if (tc == null) { throw new NoPresentationContextException( "Modality Worklist not supported by " + remoteAE.getAETitle()); } DimseRSP rsp = assoc.cfind(UID.ModalityWorklistInformationModelFIND, priority, keys, tc.getTransferSyntax()[0], cancelAfter); List<DicomObject> result = new ArrayList<DicomObject>(); while (rsp.next()) { DicomObject cmd = rsp.getCommand(); if (CommandUtils.isPending(cmd)) { DicomObject data = rsp.getDataset(); result.add(data); } } return result; } } worklist.java : public class worklist extends Interface{ public worklist(){ } public static void main(String[] args, String PatientID, String accessingNumber, String PatientName, String BirthDate, String sex, String referringPhysician ) { DefaultTableModel modelw=(DefaultTableModel)worklisttable.getModel(); modelw.setColumnIdentifiers(new String[]{"ID Patient"," Nom Patient","Date de Naissance","Sexe"}); DefaultTableCellRenderer centerRenderer= new DefaultTableCellRenderer(); centerRenderer.setHorizontalAlignment(JLabel.CENTER); worklisttable.getColumnModel().getColumn(0).setCellRenderer(centerRenderer); for (int j = 0 ; j< worklisttable.getColumnCount(); j++) { worklisttable.getColumnModel().getColumn(j).setCellRenderer(centerRenderer); } row=new Object[4]; row[0]=PatientID; row[1]=PatientName; row[2]=BirthDate; row[3]=sex; modelw.addRow(row); } }
Getting this error while parsing data in JSON format in android
I'm getting this error: org.json.JSONException: Value http of type java.lang.String cannot be converted to JSONObject public class QueryUtils { private QueryUtils(){ } //private static final String URL = "http://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=2016-01-01&endtime=2016-01-31&minmag=6&limit=10"; public static ArrayList<Earthquake> extractEarthquakes() { String URL = "http://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=2016-01-01&endtime=2016-01-31&minmag=6&limit=10"; ArrayList<Earthquake> earthquakes = new ArrayList<Earthquake>(); try { String responseDate = URL.toString(); JSONObject rootObject = new JSONObject(responseDate); JSONArray features = rootObject.getJSONArray("features"); // Date dateObject; // SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMM DD,yyyy h:mm a", Locale.ENGLISH); //iterate through loop to display each value for(int i=0; i <features.length(); i++){ JSONObject jsonObjectForDetails = features.getJSONObject(i); JSONObject jsonObjectForProperties = jsonObjectForDetails.getJSONObject("properties"); // double magValue = double.parseInt(jsonObjectForProperties.optString("mag").toString()); // String addressDetails = jsonObjectForProperties.optString("place").toString(); // int dateDetails = Integer.parseInt(jsonObjectForProperties.optString("time").toString()); double magValue = jsonObjectForProperties.getDouble("mag"); String addressDetails = jsonObjectForProperties.getString("place"); long dateDetails = jsonObjectForProperties.getLong("time"); // dateObject = new Date(dateDetails); // long dateToDisplay = simpleDateFormat.format(dateObject); earthquakes.add(new Earthquake(magValue,addressDetails,dateDetails)); } } catch (JSONException e) { Log.e("QueryUtils", "Problem parsing the earthquake JSON results", e); } return earthquakes; } }
to summarize the other comments: to retrieve data from url use this function: public static String getJsonFromServer(String url) throws IOException { BufferedReader inputStream = null; URL jsonUrl = new URL(url); URLConnection dc = jsonUrl.openConnection(); dc.setConnectTimeout(5000); dc.setReadTimeout(5000); inputStream = new BufferedReader(new InputStreamReader( dc.getInputStream())); // read the JSON results into a string String jsonResult = inputStream.readLine(); return jsonResult; } for your case use try { String responseDate= getJsonFromServer(URL); //the other code: } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } instead of String responseDate = URL.toString(); and this code (for json parsing) works too: import org.json.JSONArray; import org.json.JSONObject; public class EarthQuakeTest { //http://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=2016-01-01&endtime=2016-01-31&minmag=6&limit=10 private static final String str = "{\"type\":\"FeatureCollection\",\"metadata\":{\"generated\":1475336259000,\"url\":\"http://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=2016-01-01&endtime=2016-01-31&minmag=6&limit=10\",\"title\":\"USGS Earthquakes\",\"status\":200,\"api\":\"1.5.2\",\"limit\":10,\"offset\":1,\"count\":10},\"features\":[{\"type\":\"Feature\",\"properties\":{\"mag\":7.2,\"place\":\"88km N of Yelizovo, Russia\",\"time\":1454124312220,\"updated\":1463685050282,\"tz\":720,\"url\":\"http://earthquake.usgs.gov/earthquakes/eventpage/us20004vvx\",\"detail\":\"http://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us20004vvx&format=geojson\",\"felt\":2,\"cdi\":3.4,\"mmi\":5.82,\"alert\":\"green\",\"status\":\"reviewed\",\"tsunami\":1,\"sig\":798,\"net\":\"us\",\"code\":\"20004vvx\",\"ids\":\",gcmt20160130032510,at00o1qxho,pt16030050,us20004vvx,gcmt20160130032512,\",\"sources\":\",gcmt,at,pt,us,gcmt,\",\"types\":\",associate,cap,dyfi,finite-fault,general-link,general-text,geoserve,impact-link,impact-text,losspager,moment-tensor,nearby-cities,origin,phase-data,shakemap,tectonic-summary,\",\"nst\":null,\"dmin\":0.958,\"rms\":1.19,\"gap\":17,\"magType\":\"mww\",\"type\":\"earthquake\",\"title\":\"M 7.2 - 88km N of Yelizovo, Russia\"},\"geometry\":{\"type\":\"Point\",\"coordinates\":[158.5463,53.9776,177]},\"id\":\"us20004vvx\"},\r\n{\"type\":\"Feature\",\"properties\":{\"mag\":6.1,\"place\":\"94km SSE of Taron, Papua New Guinea\",\"time\":1453777820750,\"updated\":1466450055338,\"tz\":600,\"url\":\"http://earthquake.usgs.gov/earthquakes/eventpage/us20004uks\",\"detail\":\"http://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us20004uks&format=geojson\",\"felt\":null,\"cdi\":null,\"mmi\":4.1,\"alert\":\"green\",\"status\":\"reviewed\",\"tsunami\":1,\"sig\":572,\"net\":\"us\",\"code\":\"20004uks\",\"ids\":\",gcmt20160126031023,us20004uks,gcmt20160126031020,\",\"sources\":\",gcmt,us,gcmt,\",\"types\":\",associate,cap,geoserve,losspager,moment-tensor,nearby-cities,origin,phase-data,shakemap,tectonic-summary,\",\"nst\":null,\"dmin\":1.537,\"rms\":0.74,\"gap\":25,\"magType\":\"mww\",\"type\":\"earthquake\",\"title\":\"M 6.1 - 94km SSE of Taron, Papua New Guinea\"},\"geometry\":{\"type\":\"Point\",\"coordinates\":[153.2454,-5.2952,26]},\"id\":\"us20004uks\"},\r\n{\"type\":\"Feature\",\"properties\":{\"mag\":6.3,\"place\":\"50km NNE of Al Hoceima, Morocco\",\"time\":1453695722730,\"updated\":1466450270946,\"tz\":0,\"url\":\"http://earthquake.usgs.gov/earthquakes/eventpage/us10004gy9\",\"detail\":\"http://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us10004gy9&format=geojson\",\"felt\":117,\"cdi\":7.2,\"mmi\":5.28,\"alert\":\"green\",\"status\":\"reviewed\",\"tsunami\":0,\"sig\":695,\"net\":\"us\",\"code\":\"10004gy9\",\"ids\":\",gcmt20160125042203,us10004gy9,gcmt20160125042202,\",\"sources\":\",gcmt,us,gcmt,\",\"types\":\",associate,cap,dyfi,geoserve,impact-text,losspager,moment-tensor,nearby-cities,origin,phase-data,shakemap,tectonic-summary,\",\"nst\":null,\"dmin\":2.201,\"rms\":0.92,\"gap\":20,\"magType\":\"mww\",\"type\":\"earthquake\",\"title\":\"M 6.3 - 50km NNE of Al Hoceima, Morocco\"},\"geometry\":{\"type\":\"Point\",\"coordinates\":[-3.6818,35.6493,12]},\"id\":\"us10004gy9\"},\r\n{\"type\":\"Feature\",\"properties\":{\"mag\":7.1,\"place\":\"86km E of Old Iliamna, Alaska\",\"time\":1453631430230,\"updated\":1469293028539,\"tz\":-540,\"url\":\"http://earthquake.usgs.gov/earthquakes/eventpage/us10004gqp\",\"detail\":\"http://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us10004gqp&format=geojson\",\"felt\":1816,\"cdi\":7.2,\"mmi\":6.6,\"alert\":\"green\",\"status\":\"reviewed\",\"tsunami\":1,\"sig\":1496,\"net\":\"us\",\"code\":\"10004gqp\",\"ids\":\",at00o1gd6r,us10004gqp,ak12496371,gcmt20160124103030,\",\"sources\":\",at,us,ak,gcmt,\",\"types\":\",cap,dyfi,finite-fault,general-link,general-text,geoserve,impact-link,impact-text,losspager,moment-tensor,nearby-cities,origin,phase-data,shakemap,tectonic-summary,trump-origin,\",\"nst\":null,\"dmin\":0.72,\"rms\":2.11,\"gap\":19,\"magType\":\"mww\",\"type\":\"earthquake\",\"title\":\"M 7.1 - 86km E of Old Iliamna, Alaska\"},\"geometry\":{\"type\":\"Point\",\"coordinates\":[-153.4051,59.6363,129]},\"id\":\"us10004gqp\"},\r\n{\"type\":\"Feature\",\"properties\":{\"mag\":6.6,\"place\":\"215km SW of Tomatlan, Mexico\",\"time\":1453399617650,\"updated\":1466450324948,\"tz\":-420,\"url\":\"http://earthquake.usgs.gov/earthquakes/eventpage/us10004g4l\",\"detail\":\"http://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us10004g4l&format=geojson\",\"felt\":11,\"cdi\":2.7,\"mmi\":3.92,\"alert\":\"green\",\"status\":\"reviewed\",\"tsunami\":1,\"sig\":673,\"net\":\"us\",\"code\":\"10004g4l\",\"ids\":\",gcmt20160121180659,at00o1bebo,pt16021050,us10004g4l,gcmt20160121180657,\",\"sources\":\",gcmt,at,pt,us,gcmt,\",\"types\":\",associate,cap,dyfi,geoserve,impact-link,impact-text,losspager,moment-tensor,nearby-cities,origin,phase-data,shakemap,tectonic-summary,\",\"nst\":null,\"dmin\":2.413,\"rms\":0.98,\"gap\":74,\"magType\":\"mww\",\"type\":\"earthquake\",\"title\":\"M 6.6 - 215km SW of Tomatlan, Mexico\"},\"geometry\":{\"type\":\"Point\",\"coordinates\":[-106.9337,18.8239,10]},\"id\":\"us10004g4l\"},\r\n{\"type\":\"Feature\",\"properties\":{\"mag\":6.7,\"place\":\"52km SE of Shizunai, Japan\",\"time\":1452741933640,\"updated\":1466450357311,\"tz\":540,\"url\":\"http://earthquake.usgs.gov/earthquakes/eventpage/us10004ebx\",\"detail\":\"http://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us10004ebx&format=geojson\",\"felt\":51,\"cdi\":5.8,\"mmi\":6.45,\"alert\":\"green\",\"status\":\"reviewed\",\"tsunami\":1,\"sig\":720,\"net\":\"us\",\"code\":\"10004ebx\",\"ids\":\",us10004ebx,gcmt20160114032534,pt16014050,at00o0xauk,gcmt20160114032533,\",\"sources\":\",us,gcmt,pt,at,gcmt,\",\"types\":\",associate,cap,dyfi,geoserve,impact-link,impact-text,losspager,moment-tensor,nearby-cities,origin,phase-data,shakemap,\",\"nst\":null,\"dmin\":0.281,\"rms\":0.98,\"gap\":22,\"magType\":\"mww\",\"type\":\"earthquake\",\"title\":\"M 6.7 - 52km SE of Shizunai, Japan\"},\"geometry\":{\"type\":\"Point\",\"coordinates\":[142.781,41.9723,46]},\"id\":\"us10004ebx\"},\r\n{\"type\":\"Feature\",\"properties\":{\"mag\":6.1,\"place\":\"12km WNW of Charagua, Bolivia\",\"time\":1452741928270,\"updated\":1463165886000,\"tz\":-240,\"url\":\"http://earthquake.usgs.gov/earthquakes/eventpage/us10004ebw\",\"detail\":\"http://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us10004ebw&format=geojson\",\"felt\":3,\"cdi\":2.2,\"mmi\":2.21,\"alert\":\"green\",\"status\":\"reviewed\",\"tsunami\":0,\"sig\":573,\"net\":\"us\",\"code\":\"10004ebw\",\"ids\":\",us10004ebw,gcmt20160114032528,\",\"sources\":\",us,gcmt,\",\"types\":\",cap,dyfi,geoserve,impact-text,losspager,moment-tensor,nearby-cities,origin,phase-data,shakemap,tectonic-summary,\",\"nst\":null,\"dmin\":5.492,\"rms\":1.04,\"gap\":16,\"magType\":\"mww\",\"type\":\"earthquake\",\"title\":\"M 6.1 - 12km WNW of Charagua, Bolivia\"},\"geometry\":{\"type\":\"Point\",\"coordinates\":[-63.3288,-19.7597,582.56]},\"id\":\"us10004ebw\"},\r\n{\"type\":\"Feature\",\"properties\":{\"mag\":6.2,\"place\":\"74km NW of Rumoi, Japan\",\"time\":1452532083920,\"updated\":1463165873000,\"tz\":540,\"url\":\"http://earthquake.usgs.gov/earthquakes/eventpage/us10004djn\",\"detail\":\"http://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us10004djn&format=geojson\",\"felt\":8,\"cdi\":3.4,\"mmi\":3.74,\"alert\":\"green\",\"status\":\"reviewed\",\"tsunami\":0,\"sig\":594,\"net\":\"us\",\"code\":\"10004djn\",\"ids\":\",us10004djn,gcmt20160111170803,\",\"sources\":\",us,gcmt,\",\"types\":\",cap,dyfi,geoserve,impact-text,losspager,moment-tensor,nearby-cities,origin,phase-data,shakemap,tectonic-summary,\",\"nst\":null,\"dmin\":1.139,\"rms\":0.96,\"gap\":33,\"magType\":\"mww\",\"type\":\"earthquake\",\"title\":\"M 6.2 - 74km NW of Rumoi, Japan\"},\"geometry\":{\"type\":\"Point\",\"coordinates\":[141.0867,44.4761,238.81]},\"id\":\"us10004djn\"},\r\n{\"type\":\"Feature\",\"properties\":{\"mag\":6.5,\"place\":\"227km SE of Sarangani, Philippines\",\"time\":1452530285900,\"updated\":1466450384901,\"tz\":480,\"url\":\"http://earthquake.usgs.gov/earthquakes/eventpage/us10004dj5\",\"detail\":\"http://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us10004dj5&format=geojson\",\"felt\":1,\"cdi\":2.7,\"mmi\":7.5,\"alert\":\"green\",\"status\":\"reviewed\",\"tsunami\":1,\"sig\":650,\"net\":\"us\",\"code\":\"10004dj5\",\"ids\":\",gcmt20160111163807,at00o0srjp,pt16011050,us10004dj5,gcmt20160111163805,\",\"sources\":\",gcmt,at,pt,us,gcmt,\",\"types\":\",associate,cap,dyfi,geoserve,impact-link,impact-text,losspager,moment-tensor,nearby-cities,origin,phase-data,shakemap,tectonic-summary,\",\"nst\":null,\"dmin\":3.144,\"rms\":0.72,\"gap\":22,\"magType\":\"mww\",\"type\":\"earthquake\",\"title\":\"M 6.5 - 227km SE of Sarangani, Philippines\"},\"geometry\":{\"type\":\"Point\",\"coordinates\":[126.8621,3.8965,13]},\"id\":\"us10004dj5\"},\r\n{\"type\":\"Feature\",\"properties\":{\"mag\":6,\"place\":\"Pacific-Antarctic Ridge\",\"time\":1451986454620,\"updated\":1466450408406,\"tz\":-540,\"url\":\"http://earthquake.usgs.gov/earthquakes/eventpage/us10004bgk\",\"detail\":\"http://earthquake.usgs.gov/fdsnws/event/1/query?eventid=us10004bgk&format=geojson\",\"felt\":0,\"cdi\":1,\"mmi\":0,\"alert\":\"green\",\"status\":\"reviewed\",\"tsunami\":0,\"sig\":554,\"net\":\"us\",\"code\":\"10004bgk\",\"ids\":\",gcmt20160105093415,us10004bgk,gcmt20160105093414,\",\"sources\":\",gcmt,us,gcmt,\",\"types\":\",associate,cap,dyfi,geoserve,losspager,moment-tensor,nearby-cities,origin,phase-data,shakemap,\",\"nst\":null,\"dmin\":30.75,\"rms\":0.67,\"gap\":71,\"magType\":\"mww\",\"type\":\"earthquake\",\"title\":\"M 6.0 - Pacific-Antarctic Ridge\"},\"geometry\":{\"type\":\"Point\",\"coordinates\":[-136.2603,-54.2906,10]},\"id\":\"us10004bgk\"}],\"bbox\":[-153.4051,-54.2906,10,158.5463,59.6363,582.56]}"; public static void main(String[] args) { // TODO Auto-generated method stub JSONObject rootObject = new JSONObject(str); JSONArray features = rootObject.getJSONArray("features"); for(int i=0; i <features.length(); i++){ JSONObject jsonObjectForDetails = features.getJSONObject(i); JSONObject jsonObjectForProperties = jsonObjectForDetails.getJSONObject("properties"); double magValue = jsonObjectForProperties.getDouble("mag"); String addressDetails = jsonObjectForProperties.getString("place"); long dateDetails = jsonObjectForProperties.getLong("time"); // dateObject = new Date(dateDetails); // long dateToDisplay = simpleDateFormat.format(dateObject); // earthquakes.add(new Earthquake(magValue,addressDetails,dateDetails)); System.out.println(magValue + " " + addressDetails + " " + dateDetails); } System.out.println(rootObject.hashCode()); } }
how to remove 503 exceptions in java
I have a list of names in the form of a CSV and I am up for google searching those names using java. But the problem that i am facing is that when i initially run the code i am able to search the query but in the middle of the code the code starts to throw 503 exceptions and when i again run the code it starts throwing 503 exceptions from the very beginning.Here is the code that i am using. public class ExtractInformation { static String firstname,middlename,lastname; public static final int PAGE_NUMBERS = 10; public static void readCSV() { boolean first = true; try { String splitBy = ","; BufferedReader br = new BufferedReader(new FileReader("E:\\KOLDump\\names.csv")); String line = null; String site = null; while((line=br.readLine())!=null) { if(first) { first = false; continue; } String[] b = line.split(splitBy); firstname = b[0]; middlename = b[1]; lastname = b[2]; String name = null; if(middlename == null || middlename.length() == 0) { name = firstname+" "+lastname+" OR "+lastname+" "+firstname.charAt(0); } else { name = firstname+" "+lastname+" OR "+lastname+" "+firstname.charAt(0)+" OR "+firstname+" "+middlename.charAt(0)+". "+lastname; } BufferedReader brs = new BufferedReader(new FileReader("E:\\KOLDump\\site.csv")); while((site = brs.readLine()) != null) { if(first) { first = false; continue; } String [] s = site.split(splitBy); String siteName = s[0]; siteName = (siteName.replace("www.", "")); siteName = (siteName.replace("http://", "")); getDataFromGoogle(name.trim(), siteName.trim()); } brs.close(); } //br.close(); } catch(Exception e) { System.out.println("unable to read file...some problem in the csv"); } } public static void main(String[] args) { readCSV(); } private static void getDataFromGoogle(String query,String siteName) { Set<String> result = new HashSet<String>(); String request = "http://www.google.co.in/search?q="+query+" "+siteName; try { Document doc = Jsoup.connect(request).userAgent("Chrome").timeout(10000).get(); Element query_results = doc.getElementById("ires"); Elements gees = query_results.getElementsByClass("g"); for(Element gee : gees) { Element h3 = gee.getElementsByTag("h3").get(0); String annotation = h3.getElementsByTag("a").get(0).attr("href"); if(annotation.split("q=",2)[1].contains(siteName)) { System.out.println(annotation.split("q=",2)[1]); } } } catch (IOException e) { e.printStackTrace(); } } } any suggestions on how to remove this exceptions from the code would really be helpful.
If you wait a little do the 503's go away? If so, then you're probably being rate-limited by Google. https://support.google.com/gsa/answer/2686272?hl=en You may need to put some kind of delay between requests.
Something in AsyncTask Blocking the UI - causing interface to halt briefly
I have a listview that is populated thru SQLite with cache data. After it finishes loading. in the background I check for new data and get a returned JSON result from a MySQL db. In my onPostExecute of this background task, when this code is ran (the code below), and while it is being looped thru (a maximum of 50 loops), the UI thread is blocked and scrolling a ListView is not possible. Here is code: if (result.length() != 0) { JSONArray jArray = new JSONArray(result); JSONObject json_data = null; for (int ii = 0; ii < jArray.length(); ii++) { json_data = jArray.getJSONObject(ii); item = json_data.getString("item"); cat = json_data.getString("category"); user = json_data.getString("username"); userId = json_data.getLong("user_id"); review = json_data.getString("review"); reviewId = json_data.getLong("review_id"); itemId = json_data.getLong("item_id"); commentCount = json_data.getLong("commentCount"); url = json_data.getString("name"); url = pathUrl + url; // for profile icon date = json_data.getString("date"); rating = json_data.getDouble("rating"); upVote = json_data.getLong("good"); wiki = json_data.getString("wiki"); watchItems.add(item); watchCats.add(cat); watchUsers.add(user); watchReviews.add(review); watchUrl.add(url); watchDateList.add(date); watchWikiList.add(wiki); watchItemIdList.add(String.valueOf(itemId)); watchUserIds.add(String.valueOf(userId)); watchReviewId.add(String.valueOf(reviewId)); watchRating.add(String.valueOf(rating)); watchCommentCount.add(String.valueOf(commentCount)); watchUpVote.add(String.valueOf(upVote)); Rateit.haveFollowing = "1"; if (Rateit.isUserLoggedIn == true) { boolean oldReview = datasource .getReviewIds(reviewId); if (!oldReview) { // Cache Network Items datasource.createTrendWatch(itemId, item, review, reviewId, cat, user, String.valueOf(userId), url, date, commentCount, rating, upVote, 0, wiki); } } FollowingItems wti = new FollowingItems( Long.valueOf(watchItemIdList.get(i)), watchItems.get(i), watchCats.get(i), watchReviews.get(i), Long.valueOf(watchReviewId.get(i)), watchUsers.get(i), Long.valueOf(watchUserIds.get(i)), watchUrl.get(i), watchDateList.get(i), Long.valueOf(watchCommentCount.get(i)), Double.valueOf(watchRating.get(i)), Long.valueOf(watchUpVote.get(i)), watchWikiList.get(i++)); watchingListObject.add(wti); } } Why is this happening? And how can I prevent my code to prevent this? Are there any optimizations I can make? Edit: Someone below requested full task code. Below repeats the code above but in context with entire task. public static class FollowingTask extends AsyncTask<String, String, Void> { protected InputStream is = null; protected String result = ""; protected String userId; protected ArrayList<FollowingItems> watchingListObject; protected Context mContext; public FollowingTask(Context context) { mContext = context; } #Override protected void onPreExecute() { if (mContext != null && (fromRefresh == false)) { ((MainFragmentActivity) mContext) .setSupportProgressBarIndeterminateVisibility(true); } resetLists(); if (PrefActivity.getUserLoggedInStatus(mContext) == true) { userId = PrefActivity.getLoggedInUserId(mContext); } else { userId = "-1"; } super.onPreExecute(); } #Override protected Void doInBackground(String... params) { datasource.purgeItemWatchingTable(); Log.d("1", "Back"); String url_select = "http://www.---.info/includes_mc_php/featured_watching.php"; HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url_select); ArrayList<NameValuePair> param = new ArrayList<NameValuePair>(); param.add(new BasicNameValuePair("user_id", userId)); param.add(new BasicNameValuePair("v2", "true")); try { httpPost.setEntity(new UrlEncodedFormEntity(param)); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); // read content is = httpEntity.getContent(); } catch (Exception e) { e.printStackTrace(); } try { BufferedReader br = new BufferedReader( new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = ""; while ((line = br.readLine()) != null) { sb.append(line + "\n"); } is.close(); result = sb.toString(); } catch (Exception e) { e.printStackTrace(); } return null; } protected void onPostExecute(Void v) { String pathUrl = Rateit.PROFILE_PIC_URL; String item, cat, user, review, url, date, following, wiki; long itemId, reviewId, userId, commentCount, upVote; double rating; int i = 0; watchingListObject = new ArrayList<FollowingItems>(); try { String c = String.valueOf(result.charAt(0)); if (c.equals("{")) { JSONObject jsonObject = new JSONObject(result); following = jsonObject.getString("following"); if (following.equals("0")) { Rateit.haveFollowing = "0"; } } else { if (result.length() != 0) { JSONArray jArray = new JSONArray(result); JSONObject json_data = null; for (int ii = 0; ii < jArray.length(); ii++) { json_data = jArray.getJSONObject(ii); item = json_data.getString("item"); cat = json_data.getString("category"); user = json_data.getString("username"); userId = json_data.getLong("user_id"); review = json_data.getString("review"); reviewId = json_data.getLong("review_id"); itemId = json_data.getLong("item_id"); commentCount = json_data.getLong("commentCount"); url = json_data.getString("name"); url = pathUrl + url; // for profile icon date = json_data.getString("date"); rating = json_data.getDouble("rating"); upVote = json_data.getLong("good"); wiki = json_data.getString("wiki"); watchItems.add(item); watchCats.add(cat); watchUsers.add(user); watchReviews.add(review); watchUrl.add(url); watchDateList.add(date); watchWikiList.add(wiki); watchItemIdList.add(String.valueOf(itemId)); watchUserIds.add(String.valueOf(userId)); watchReviewId.add(String.valueOf(reviewId)); watchRating.add(String.valueOf(rating)); watchCommentCount.add(String.valueOf(commentCount)); watchUpVote.add(String.valueOf(upVote)); Rateit.haveFollowing = "1"; if (Rateit.isUserLoggedIn == true) { boolean oldReview = datasource .getReviewIds(reviewId); if (!oldReview) { // Cache Network Items datasource.createTrendWatch(itemId, item, review, reviewId, cat, user, String.valueOf(userId), url, date, commentCount, rating, upVote, 0, wiki); } } FollowingItems wti = new FollowingItems( Long.valueOf(watchItemIdList.get(i)), watchItems.get(i), watchCats.get(i), watchReviews.get(i), Long.valueOf(watchReviewId.get(i)), watchUsers.get(i), Long.valueOf(watchUserIds.get(i)), watchUrl.get(i), watchDateList.get(i), Long.valueOf(watchCommentCount.get(i)), Double.valueOf(watchRating.get(i)), Long.valueOf(watchUpVote.get(i)), watchWikiList.get(i++)); watchingListObject.add(wti); Log.d("1", "Post 2"); } } else { Rateit.haveFollowing = "2"; } } } catch (JSONException e1) { e1.printStackTrace(); Rateit.haveFollowing = "2"; } catch (ParseException e1) { e1.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } Log.d("1", "Post COMPLETE"); mPullRefreshListView.onRefreshComplete(); // Reset Trending List on Pull-to-Refresh if (mContext != null) { if (watchUsers.size() == 0) { l.setVisibility(View.VISIBLE); tv.setTypeface(TypeFace.get(mContext, Rateit.BPREPLAY)); } else { l.setVisibility(View.GONE); } if (mContext != null) { listView.setAdapter(null); if (watchItems.size() > 0) { wAdapter = new FollowingAdapter(mContext, watchingListObject, TypeFace.get(mContext, Rateit.BPREPLAY), TypeFace.get( mContext, Rateit.ROBOTO_LIGHT), TypeFace.get(mContext, Rateit.ROBOTO_THIN), TypeFace.get(mContext, Rateit.ROBOTO_REGULAR)); listView.setAdapter(wAdapter); } } } if (mContext != null && (fromRefresh == false)) { ((MainFragmentActivity) mContext) .setSupportProgressBarIndeterminateVisibility(false); MainFragmentActivity.dismissDialog(mContext); } fromRefresh = false; } }
onPostExecute runs on the UI thread. It will block the UI. doInBackground runs in the background. You should perform heavy opertaions in the doInBackground (not in onPostExecute) Solution: you should move the parsing etc. from the onPostExecute to doInBackground and use the onPostExecute just for binding the processed information to the UI.
I would suggest as first thing to profile that code and measure how much time exactly is spent to execute it. This way at least you understand if your problem is really here or somewhere else
Passing values in runnable class constructor in Java
I hope somebody could please help me out in identifying to where I am going wrong with the codes below. public class CCFileImpl implements CCFileAbs { private LogMe logMe = null; private ExecutorService ccfileimpl_exsc = null; private CCProcessorImpl cProc = null; private DataUtil dUtil = null; public CCFileImpl() { this.logMe = LogMe.getLogger(); if (dUtil == null) { dUtil = new DataUtil(); } } #Override public void getFilesForProcess() { CCHeader cHead = null; Future future = null; String sPath = PropReader.getPropValue(PropReader.FILEDIR); //D:\samples\ int iCtr = 0; ccfileimpl_exsc = Executors.newFixedThreadPool(Integer.parseInt(PropReader.getPropValue(PropReader.TPool_File))); Date dToday = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy"); Iterator iter = dUtil.getFilesForProcess(sdf.format(dToday)).iterator(); String sFileGroupName = "", sFileName = ""; String sId = null; //"testFiles"; while (iter.hasNext()) { cHead = (CCHeader) iter.next(); sFileName = cHead.getsFileName(); //(String) iter.next(); sId = cHead.getsId(); sFileGroupName = sFileName + "_" + iCtr++; dUtil.updTPDHDRStatusById(sId); //Interface utility class // <=== And also here, when trying to update the db // nothing happened. cProc = new CCProcessorImpl(sId, sFileGroupName, sPath, sFileName); // <=== Problem is here? future = ccfileimpl_exsc.submit(cProc); } ccfileimpl_exsc.shutdown(); } } The above code retrieves the files for processing then assigning it to a runnable class (below) then submitting it to an executorService class. Now i can't understand to why the passed values of the constructor (below) is set to null/space and only the sPath variable has a definite value. public class CCProcessorImpl implements Runnable{ private CCFileParser rpsCCParser; private ExecutorService ccprocimpl_exsc; private static LogMe logMe; private final String sGroupName; private final String sId; private final String sFileName; #Override public void run() { this.parseFiles(sId, sFileName); } public CCProcessorImpl(String sId, String sGroupName, String sPath, String sFileName) { this.logMe = LogMe.getLogger(); this.sId = sId; this.sGroupName = sGroupName; this.sFileName = sPath + sFileName; } public void parseFiles(String sId, String sFileName) { try { Future future = null; rpsCCParser = new CCFileParser(sId, sFileName); ArrayList aList = rpsCCParser.getFileContent(); String sGroupName = sId + "_"; ccprocimpl_exsc = Executors.newFixedThreadPool(Integer.parseInt(PropReader.getPropValue(PropReader.TPool_Content))); int iStart = 0, iSize = 9, iEnd = iSize; for (int iCtr = 0; iCtr <= ((aList.size() / 10) - 1); iCtr++, iStart += iSize, iEnd += iSize) { future = ccprocimpl_exsc.submit(new CCUpdater(aList.subList(iStart, iEnd), sGroupName + iCtr)); } future.get(); ccprocimpl_exsc.shutdown(); } catch (ExecutionException e) { throw new RuntimeException(e); } catch (InterruptedException ie) { throw new RuntimeException(ie); } } } Also as a supplementary question, why is it when i tried to update the db table no updates were performed? Would this be related to being in a thread environment?
Why don't you use the futures returned by ccfileimpl_exsc.submit()? Instead, you call ccfileimpl_exsc.shutdown() right after submitting the jobs, killing them before they finish.