0

Here is the code. Ill post this in the snippet section as well:

private void button1_Click(object sender, EventArgs e)
        {
            string newString = "";
            var client = new RestClient("http://www.daniweb.com/api"); /*I prepare the API*/

            /*I remove whitespaces except actual spaces*/
            for (int i = 0; i < textBox1.Text.Length; i++)
            {
                if ((char.IsWhiteSpace(textBox1.Text[i])) && (!textBox1.Text[i].Equals(' ')))
                {
                    textBox1.Text = textBox1.Text.Remove(i, 1); 
                }

            }


            /*I make sure that the user name isnt empty */
            if (textBox1.TextLength==0)
            {
                MessageBox.Show("Please insert a valid member");
            }
            else
            {

            /*I call the members method of the API and pass parameters by GET. POST did not work (Why?)*/
            var request = new RestRequest("members", Method.GET);
            request.AddParameter("username", textBox1.Text);

            /*Send it and wait for reply*/
            var response = client.Execute(request);

            /*Finding where it says the post number in the response...*/
            int next = 0;
            int first = response.Content.IndexOf("\"posts\":\"");
                if (first==-1) /*-1 means it could not find it therefore no member exists*/
                {
                    MessageBox.Show("Member not found!"); 
                }
                else
                {
                    /*I find the next quote that actually begins the post number*/
            do
            {
                next = response.Content.IndexOf("\"", first);
                /*Not neccesary but in case Dani changes the array to something else....*/
                if (next==-1)
                {
                    break;
                }
            } while ((next==0));



            /*VERY Ugly hack to get posts numbers. Tried with largest post numbers I could find (Dani's AFAIK)*/
            string str2 = response.Content.Substring(first, (next + 16) - first);
            newString = Regex.Replace(str2, "[^.0-9]", "");

            }

             if (textBox1.TextLength == 0)
             {
                 MessageBox.Show("Please insert a valid member");
             }
             else if (first!=-1)
             {
                 MessageBox.Show(newString, "The total amount of posts this user has is:");
             }
            }

        }

Like I mentioned, very dirty and just to work. Can be done way better. 4.0

Also the /**/ Are used to make it "copy/paste" ready...

Edited by riahc3

0

Two things I forgot; Most important IMO:

Thanks to Tekmaven for the idea on how to implement this in C#
Thanks to ddanbe for the spaces fix

0

God, I cant wait to write a client to post on Daniweb. This is just frustrating that you can make a simply snippet post...

0

I cant post it to the snippet section. Like Ive mentioned over and over, posting on Daniweb completely blows. Someone can post the code in the snippet section, although personally its not to my liking but for the good of the community....

Just give me credit, obviously :)

0

OK Ive been able to post it but without the text that I put after. What is wrong with it?

0

Cheers Riahc3 :)

I want to cut my vains out because of the frustration that is posting on Daniweb!!!! :P

If you find any bugs/glitches please comment.

0

OK Ive been able to post it but without the text that I put after. What is wrong with it?

Cross posting your question makes it more likely that you'll miss the answer. Please see your other thread on the subject for my response.

0

Dani, Im going to send you a PM about a issue Im facing with the API. Im not sure if you want it to be visible so just in case......Its problably nothing.

0

This is why I hate Java :P

Im porting my C# dummy program to Java as well but it seems its putting up some fights.

Thing is I spend 99% of my time (working) on Java and about 1% of my time as a hobby on C#!

BTW, ignore my PM Dani. I figured out what was happening.

0

I've been working on a Java API that tries to simplify accessing the Daniweb API from Java based applications.
It isn't finished yet and still has bugs. I attached to my post what's basically an export of my Eclipse project.
I release it because at the moment I lack the time to finish the implementation and because there doesn't seem to be alot of Java activity around the Daniweb API.
I hope the release of this source will encourage the Java people to experiment with Java and the Daniweb API.
Later when I will have time to finish it I'll clean it up and post it down as a code snippet.
Any suggestions, fixes and constructive critique are always welcomed and highly appreciated. ;)

Edited by mvmalderen

0

Quick example:

// Fetch member entity of Dani
Member dani = RequestBuilder.memberRequest().username("dani").build().fetchSingle();

// Fetch Java code snippets by Dani and order them last post first
FetchRequest<Article> request = RequestBuilder.articleRequest()
    .members(dani.getUserId())
    .forums(9)
    .orderBy(LASTPOST)
    .filter(CODE)
    .build();

List<Article> articles = request.fetchMultiple();

// Fetch Forum data of the C/C++/Java forums
List<Forum> forums = RequestBuilder.forumRequest().ids(118, 8, 9).build().fetchMultiple();

Edited by mvmalderen

0

I know you people are going to kill me, but I've made another change that will affect everyone.

All results are now within an array key called [items] ... so instead of it being $results['username'] it's going to be $results['items']['username']. This leaves room for other things such as meta data to be sent in the resultset in the future.

0

I know you people are going to kill me, but I've made another change that will affect everyone.

I don't see why we would kill you... we would only support you.

1

I know you people are going to kill me, but I've made another change that will affect everyone.

I think most members will be civil disobedience for few hours then back to normal again.

Edited by LastMitch: grammer

2

I think most members will be civil disobedience for few hours then back to normal again.

No, I think they will all be very API about it... ;-)

2

I don't see why we would kill you... we would only support you.

In that case, I'm changing lots of other things as well :)

I've been speaking with a friend with a lot of API experience, and he's suggesting some major revamps to the actual output. Right now, it's all just pretty much a dump of the way everything is represented in the database. He's suggesting some things to make it more human friendly and encapsulate related objects.

1

OK, I'll put my tinkering on hold. I'll come back to it after you've finished. :(

Votes + Comments
You're not the only one having such thoughts ;)
0

Everything is done except for when pulling forums. The data is all now much more organized and human-friendly!!

0

Got a Java snippet working too :) Will post soon.

Only two languages I dominate (somewhat :P ) so thats the only contributions I can give to help others develop apps.

You need the Jersey library for this.

Edited by riahc3

0

Since its a console app, Ill just post the code. Copy/paste and it should work as long as you have the library it should work.

0

And here it is:

package DaniWebClient;


import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.ws.rs.core.MultivaluedMap;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.core.util.MultivaluedMapImpl;

public class DaniWebClient 
{


    public static void main(String[] args) 
    {
        /*I prepare the API*/
        Client client = Client.create();
        WebResource webResource = client.resource("http://www.daniweb.com/api/members");

        /*User introduces name*/
        System.out.println("Enter the member who's post you want to see: ");
        String user;     
        Scanner scanIn = new Scanner(System.in);
        user = scanIn.nextLine();
        StringBuilder username=new StringBuilder(user);

            /*I remove whitespaces except actual spaces*/
            for (int i=0;i<username.length();i++)
            {
                if ((Character.isWhitespace(username.charAt(i))) && (username.charAt(i)!=' '))
                        {
                            username.deleteCharAt(i);
                        }
            }
            user = username.toString();
            /*For some stupid reason, Java is stupid (no surprise there) and doesnt consider ALT+0160 a whitespace. I have to remove it manually*/
            user = user.replace("\u00A0","");

            /*I make sure that the user name isnt empty */
            if (user.length()==0)
            {
                System.out.println("Please insert a valid member");
            }
            else
            {

                /*I call the members method of the API and pass parameters by GET. POST not tested in Java*/
                MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl();
                queryParams.add("username", user);

                /*Send it and wait for reply*/
                String output = webResource.queryParams(queryParams).get(String.class);

                int first=0;
                int next=0;

                /*Finding where it says the post number in the response...*/
                first=output.indexOf("\"posts\":\"");

                if (first==-1) /*-1 means it could not find it therefore no member exists*/
                {
                    System.out.println("Member not found!");
                }
                else
                {
                    do
                    {
                        /*I find the next quote that actually begins the post number*/
                        next = output.indexOf("\"",Integer.valueOf(first));

                        /*Not neccesary but in case Dani changes the array to something else....*/
                        if (next==-1)
                        {
                            break;
                        }

                    }while(next==0);

                     /*VERY Ugly hack to get posts numbers. Tried with largest post numbers I could find (Dani's AFAIK)*/
                    String str2 = output.substring(first, next +15);
                    Pattern replace = Pattern.compile("[^.0-9]");
                    Matcher matcher2 = replace.matcher(str2);
                    String stringne=matcher2.replaceAll("");

                    if (output.length()==0)
                    {
                        System.out.println("Please insert a valid member");
                    }
                    else
                    {
                        System.out.println("The total amounts of posts this member has is " + stringne );
                    }

                }


            }

    }

}
0

I couldn't help myself...

Tried the jsonp:

function getJSON(){
    var url = 'http://www.daniweb.com/api/articles';
    $.getJSON(url + '?callback=?').done(function(json){
        document.write(JSON.stringify(json));       
    });
}

getJSON();

But kept getting this:

{"data":{"error":"Cachebuster detected: Please pass in an access token to retrieve non-cached results."}}

I noticed this happened with the 'nested' parameter for forums too (just straight url into browser bar) - is this expected?

A bit odd, as I got data from the direct url into browser:

http://www.daniweb.com/api/articles?callback=? 

Edited by diafol

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.